%global _empty_manifest_terminate_build 0 Name: python-environment-variables Version: 0.1.0 Release: 1 Summary: Load environment variables Enum style License: MIT License URL: https://github.com/jtaxen/environment_variables Source0: https://mirrors.aliyun.com/pypi/web/packages/de/00/f7abe74e54224e752bef9fe317df12603a8621c8d9c183db26fe9ad09368/environment-variables-0.1.0.tar.gz BuildArch: noarch %description # Environment variables ![pytest](https://github.com/jtaxen/environment_variables/actions/workflows/pytest.yml/badge.svg) [![pypi](https://img.shields.io/pypi/v/environment_variables.svg)](https://pypi.python.org/project/environment_variables) [![license](https://img.shields.io/github/license/jtaxen/environment_variables.svg)](https://github.com/jtaxen/environment_variables/blob/main/LICENSE) Enum style access to environment variables with type annotations ~ Av vars och env efter förmåga, åt vars och env efter behov ~ The package is hosted at [PyPI](https://pypi.org/project/environment-variables/) ## Documentation The documentation can be found on [ReadTheDocs](https://environment-variables.readthedocs.io/en/latest/) ## Requirements This package supports Python 3.7 or later ## Installation Install using ``pip``: ```shell $ pip install environment-variables ``` ## Usage Define your environment variables as class attributes with type annotation: ```python from environment_variables import environment_variables @environment_variables class Environment: MY_VARIABLE: str MY_INTEGER: int = 10 MY_FEATURE_FLAG: bool = False ``` When accessing a class attribute, the class will automatically check the system for a environment variable of the same name and return its value cast to the annotated type. If it is not defined, the default value will be used instead. It is also possible to annotate a class attribute with any class using the `variables` function: ```python from environment_variables import environment_variables, variable @environment_variables class Environment: MY_VARIABLE: CustomClass = variable( CustomClass, default='some default value', default_factory=custom_class_factory, args=(1, 2, 3,), kwargs={'more_custom': True}, ) ``` ## The problem this is trying to solve When configuring a python program with environment variables, one would typically access them in a fashion similar to this: ```python import os my_value = os.getenv('MY_VALUE', default=123) ``` This leaves a lot of strings lying around in the code, and it gets hard to keep track on which values are being used and what variables are needed to be set when. A better approach would be to collect everything in a config file: ```python import os class MyConfig: @classmethod def get_my_value(cls, default): return os.getenv('MY_VALUE', default=default) ``` This makes it slightly easier to keep track of, but we are still using strings that we have to keep track of. An even better approach would be to use Enums: ```python import os import enum class MyVariables(enum.Enum): MY_VALUE = 'MY_VALUE' class MyConfig: @classmethod def get_my_value(cls, default): return os.getenv(MyVariables.MY_VALUE.value, default=default) ``` Much better, now we can just look at the enum to see what variables we have, but there is a lot of boilerplate code. For instance, do we really have to write out 'MY_VALUE' twice in the enum definition? It would be much more convenient to have the 'MyVaribles' class understand that the attribute name should be the environment variable to look for, instead of having to specify the string name of the variable again. On top of that, `os.getenv` always returns strings, so we would have to take care of the type casting ourselves if we want to have server ports as integers or feature flags as booleans. %package -n python3-environment-variables Summary: Load environment variables Enum style Provides: python-environment-variables BuildRequires: python3-devel BuildRequires: python3-setuptools BuildRequires: python3-pip %description -n python3-environment-variables # Environment variables ![pytest](https://github.com/jtaxen/environment_variables/actions/workflows/pytest.yml/badge.svg) [![pypi](https://img.shields.io/pypi/v/environment_variables.svg)](https://pypi.python.org/project/environment_variables) [![license](https://img.shields.io/github/license/jtaxen/environment_variables.svg)](https://github.com/jtaxen/environment_variables/blob/main/LICENSE) Enum style access to environment variables with type annotations ~ Av vars och env efter förmåga, åt vars och env efter behov ~ The package is hosted at [PyPI](https://pypi.org/project/environment-variables/) ## Documentation The documentation can be found on [ReadTheDocs](https://environment-variables.readthedocs.io/en/latest/) ## Requirements This package supports Python 3.7 or later ## Installation Install using ``pip``: ```shell $ pip install environment-variables ``` ## Usage Define your environment variables as class attributes with type annotation: ```python from environment_variables import environment_variables @environment_variables class Environment: MY_VARIABLE: str MY_INTEGER: int = 10 MY_FEATURE_FLAG: bool = False ``` When accessing a class attribute, the class will automatically check the system for a environment variable of the same name and return its value cast to the annotated type. If it is not defined, the default value will be used instead. It is also possible to annotate a class attribute with any class using the `variables` function: ```python from environment_variables import environment_variables, variable @environment_variables class Environment: MY_VARIABLE: CustomClass = variable( CustomClass, default='some default value', default_factory=custom_class_factory, args=(1, 2, 3,), kwargs={'more_custom': True}, ) ``` ## The problem this is trying to solve When configuring a python program with environment variables, one would typically access them in a fashion similar to this: ```python import os my_value = os.getenv('MY_VALUE', default=123) ``` This leaves a lot of strings lying around in the code, and it gets hard to keep track on which values are being used and what variables are needed to be set when. A better approach would be to collect everything in a config file: ```python import os class MyConfig: @classmethod def get_my_value(cls, default): return os.getenv('MY_VALUE', default=default) ``` This makes it slightly easier to keep track of, but we are still using strings that we have to keep track of. An even better approach would be to use Enums: ```python import os import enum class MyVariables(enum.Enum): MY_VALUE = 'MY_VALUE' class MyConfig: @classmethod def get_my_value(cls, default): return os.getenv(MyVariables.MY_VALUE.value, default=default) ``` Much better, now we can just look at the enum to see what variables we have, but there is a lot of boilerplate code. For instance, do we really have to write out 'MY_VALUE' twice in the enum definition? It would be much more convenient to have the 'MyVaribles' class understand that the attribute name should be the environment variable to look for, instead of having to specify the string name of the variable again. On top of that, `os.getenv` always returns strings, so we would have to take care of the type casting ourselves if we want to have server ports as integers or feature flags as booleans. %package help Summary: Development documents and examples for environment-variables Provides: python3-environment-variables-doc %description help # Environment variables ![pytest](https://github.com/jtaxen/environment_variables/actions/workflows/pytest.yml/badge.svg) [![pypi](https://img.shields.io/pypi/v/environment_variables.svg)](https://pypi.python.org/project/environment_variables) [![license](https://img.shields.io/github/license/jtaxen/environment_variables.svg)](https://github.com/jtaxen/environment_variables/blob/main/LICENSE) Enum style access to environment variables with type annotations ~ Av vars och env efter förmåga, åt vars och env efter behov ~ The package is hosted at [PyPI](https://pypi.org/project/environment-variables/) ## Documentation The documentation can be found on [ReadTheDocs](https://environment-variables.readthedocs.io/en/latest/) ## Requirements This package supports Python 3.7 or later ## Installation Install using ``pip``: ```shell $ pip install environment-variables ``` ## Usage Define your environment variables as class attributes with type annotation: ```python from environment_variables import environment_variables @environment_variables class Environment: MY_VARIABLE: str MY_INTEGER: int = 10 MY_FEATURE_FLAG: bool = False ``` When accessing a class attribute, the class will automatically check the system for a environment variable of the same name and return its value cast to the annotated type. If it is not defined, the default value will be used instead. It is also possible to annotate a class attribute with any class using the `variables` function: ```python from environment_variables import environment_variables, variable @environment_variables class Environment: MY_VARIABLE: CustomClass = variable( CustomClass, default='some default value', default_factory=custom_class_factory, args=(1, 2, 3,), kwargs={'more_custom': True}, ) ``` ## The problem this is trying to solve When configuring a python program with environment variables, one would typically access them in a fashion similar to this: ```python import os my_value = os.getenv('MY_VALUE', default=123) ``` This leaves a lot of strings lying around in the code, and it gets hard to keep track on which values are being used and what variables are needed to be set when. A better approach would be to collect everything in a config file: ```python import os class MyConfig: @classmethod def get_my_value(cls, default): return os.getenv('MY_VALUE', default=default) ``` This makes it slightly easier to keep track of, but we are still using strings that we have to keep track of. An even better approach would be to use Enums: ```python import os import enum class MyVariables(enum.Enum): MY_VALUE = 'MY_VALUE' class MyConfig: @classmethod def get_my_value(cls, default): return os.getenv(MyVariables.MY_VALUE.value, default=default) ``` Much better, now we can just look at the enum to see what variables we have, but there is a lot of boilerplate code. For instance, do we really have to write out 'MY_VALUE' twice in the enum definition? It would be much more convenient to have the 'MyVaribles' class understand that the attribute name should be the environment variable to look for, instead of having to specify the string name of the variable again. On top of that, `os.getenv` always returns strings, so we would have to take care of the type casting ourselves if we want to have server ports as integers or feature flags as booleans. %prep %autosetup -n environment-variables-0.1.0 %build %py3_build %install %py3_install install -d -m755 %{buildroot}/%{_pkgdocdir} if [ -d doc ]; then cp -arf doc %{buildroot}/%{_pkgdocdir}; fi if [ -d docs ]; then cp -arf docs %{buildroot}/%{_pkgdocdir}; fi if [ -d example ]; then cp -arf example %{buildroot}/%{_pkgdocdir}; fi if [ -d examples ]; then cp -arf examples %{buildroot}/%{_pkgdocdir}; fi pushd %{buildroot} if [ -d usr/lib ]; then find usr/lib -type f -printf "\"/%h/%f\"\n" >> filelist.lst fi if [ -d usr/lib64 ]; then find usr/lib64 -type f -printf "\"/%h/%f\"\n" >> filelist.lst fi if [ -d usr/bin ]; then find usr/bin -type f -printf "\"/%h/%f\"\n" >> filelist.lst fi if [ -d usr/sbin ]; then find usr/sbin -type f -printf "\"/%h/%f\"\n" >> filelist.lst fi touch doclist.lst if [ -d usr/share/man ]; then find usr/share/man -type f -printf "\"/%h/%f.gz\"\n" >> doclist.lst fi popd mv %{buildroot}/filelist.lst . mv %{buildroot}/doclist.lst . %files -n python3-environment-variables -f filelist.lst %dir %{python3_sitelib}/* %files help -f doclist.lst %{_docdir}/* %changelog * Thu Jun 08 2023 Python_Bot - 0.1.0-1 - Package Spec generated