env2dict


Nameenv2dict JSON
Version 0.0.3 PyPI version JSON
download
home_pagehttps://github.com/PasaOpasen/py-env-parser
SummaryEnvironment to python dictionary parser util
upload_time2024-01-06 11:45:42
maintainerDemetry Pascal
docs_urlNone
authorDemetry Pascal
requires_python
licenseMIT
keywords env environment
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            [![PyPI version](https://badge.fury.io/py/env2dict.svg)](https://pypi.org/project/env2dict/)
[![Downloads](https://pepy.tech/badge/env2dict)](https://pepy.tech/project/env2dict)
[![Downloads](https://pepy.tech/badge/env2dict/month)](https://pepy.tech/project/env2dict)
[![Downloads](https://pepy.tech/badge/env2dict/week)](https://pepy.tech/project/env2dict)

- [Environment to python dictionary parser util](#environment-to-python-dictionary-parser-util)
  - [About](#about)
  - [Syntax](#syntax)
  - [Examples](#examples)
  - [How to use](#how-to-use)
  - [More info](#more-info)


# Environment to python dictionary parser util

```sh
pip install env2dict
```

## About

This small package provides an ability of easy setting/overriding Python variables from environment. It is expected that u will use to override your configuration data without changing configuration files itself, what is especially useful for containers-oriented applications.

## Syntax

To use it, u need to define environment variables matches the pattern: **Prefix***Body***OperationSuffix** where:
* **Prefix** is any word to determine target variables; for instance, `DD` prefix means to use only variables starts with `DD`; can be empty, what means to select all available variables (not recommended) 
* *Body* is the name of the target configuration parameter
* **OperationSuffix** is the one of next available suffixes (by default, but u can change your defaults):
  * `_NUMBER` to convert variable value to integer (environment variables are always strings)
  * `_FLOAT` to convert variable value to float
  * `_FLAG` to convert variable value to `optional[boolean]` at that values:
    *  `1`/`yes`/`Yes`/`True`/`true` equal `true`
    *  `0`/`no`/`No`/`False`/`false` equal `false`
    *  `None`/`null`/`NULL` equal `null`
  * `_LIST` means to parse variable value to string list
  * `_LIST_APPEND` means to parse variable value to string list and append to existing list instead of override
  * `_JSON` means to parse variable value as json string
  * no suffix means that no conversion will be performed, so variable value will stay a string

Moreover, u can put nested dicts values using `__` separator in the environment variable name.

Note also that u can combine these suffixes to perform more complicated transformations.

## Examples

* env variable `DD_S_COUNT_NUMBER=10` will be converted to `S_COUNT=10` Python object
* `DD_S_COUNT=10` 🠚 `S_COUNT="10"`
* `DD_USE_THIS_FLAG=yes` 🠚 `USE_THIS=True`
* `DD_USE_THIS_FLAG=true` 🠚 `USE_THIS=True`
* `DD_USE_THIS_FLAG=no` 🠚 `USE_THIS=False`
* `DD_ALLOWED_HOSTS_LIST_APPEND=127.0.0.1;dev.ocr.com;dev.web.com` will append a list `['127.0.0.1', 'dev.ocr.com', 'dev.web.com']` to `ALLOWED_HOSTS` variable
* `DD_READ_ME_JSON={\"a\": 1, \"b\": [1, 2]}` will be translated to `READ_ME={'a': 1, 'b': [1, 2]}`
* `DD_SOME_DICT__KEY1__KEY2=postgres` will create a dictionary `SOME_DICT={'KEY1': {'KEY2': 'postgres'}}` if it doesn't exist and will add a field in existing dictionary by aforementioned route
* `DD_A__B_LIST_APPEND_JSON=[1, 2, 3, [1, 2, \"3\"]]` will append `[1, 2, 3, [1, 2, "3"]]` to the end of value `V` from `A={"B": V}`

## How to use

```python
from env2dict import parse_vars

new_vars = parse_vars(
    prefix='DD_',
    initial_vars=None,
    source=None
)
```

## More info

Please take a look at: 
* `parse_vars` function docstring
* `tests` directory of this package repo.


            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/PasaOpasen/py-env-parser",
    "name": "env2dict",
    "maintainer": "Demetry Pascal",
    "docs_url": null,
    "requires_python": "",
    "maintainer_email": "",
    "keywords": "env,environment",
    "author": "Demetry Pascal",
    "author_email": "qtckpuhdsa@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/53/cb/a2b8fa098660f51750459e39471da68e9ae221a8f475d78c035c7801919e/env2dict-0.0.3.tar.gz",
    "platform": null,
    "description": "[![PyPI version](https://badge.fury.io/py/env2dict.svg)](https://pypi.org/project/env2dict/)\n[![Downloads](https://pepy.tech/badge/env2dict)](https://pepy.tech/project/env2dict)\n[![Downloads](https://pepy.tech/badge/env2dict/month)](https://pepy.tech/project/env2dict)\n[![Downloads](https://pepy.tech/badge/env2dict/week)](https://pepy.tech/project/env2dict)\n\n- [Environment to python dictionary parser util](#environment-to-python-dictionary-parser-util)\n  - [About](#about)\n  - [Syntax](#syntax)\n  - [Examples](#examples)\n  - [How to use](#how-to-use)\n  - [More info](#more-info)\n\n\n# Environment to python dictionary parser util\n\n```sh\npip install env2dict\n```\n\n## About\n\nThis small package provides an ability of easy setting/overriding Python variables from environment. It is expected that u will use to override your configuration data without changing configuration files itself, what is especially useful for containers-oriented applications.\n\n## Syntax\n\nTo use it, u need to define environment variables matches the pattern: **Prefix***Body***OperationSuffix** where:\n* **Prefix** is any word to determine target variables; for instance, `DD` prefix means to use only variables starts with `DD`; can be empty, what means to select all available variables (not recommended) \n* *Body* is the name of the target configuration parameter\n* **OperationSuffix** is the one of next available suffixes (by default, but u can change your defaults):\n  * `_NUMBER` to convert variable value to integer (environment variables are always strings)\n  * `_FLOAT` to convert variable value to float\n  * `_FLAG` to convert variable value to `optional[boolean]` at that values:\n    *  `1`/`yes`/`Yes`/`True`/`true` equal `true`\n    *  `0`/`no`/`No`/`False`/`false` equal `false`\n    *  `None`/`null`/`NULL` equal `null`\n  * `_LIST` means to parse variable value to string list\n  * `_LIST_APPEND` means to parse variable value to string list and append to existing list instead of override\n  * `_JSON` means to parse variable value as json string\n  * no suffix means that no conversion will be performed, so variable value will stay a string\n\nMoreover, u can put nested dicts values using `__` separator in the environment variable name.\n\nNote also that u can combine these suffixes to perform more complicated transformations.\n\n## Examples\n\n* env variable `DD_S_COUNT_NUMBER=10` will be converted to `S_COUNT=10` Python object\n* `DD_S_COUNT=10` \ud83e\udc1a `S_COUNT=\"10\"`\n* `DD_USE_THIS_FLAG=yes` \ud83e\udc1a `USE_THIS=True`\n* `DD_USE_THIS_FLAG=true` \ud83e\udc1a `USE_THIS=True`\n* `DD_USE_THIS_FLAG=no` \ud83e\udc1a `USE_THIS=False`\n* `DD_ALLOWED_HOSTS_LIST_APPEND=127.0.0.1;dev.ocr.com;dev.web.com` will append a list `['127.0.0.1', 'dev.ocr.com', 'dev.web.com']` to `ALLOWED_HOSTS` variable\n* `DD_READ_ME_JSON={\\\"a\\\": 1, \\\"b\\\": [1, 2]}` will be translated to `READ_ME={'a': 1, 'b': [1, 2]}`\n* `DD_SOME_DICT__KEY1__KEY2=postgres` will create a dictionary `SOME_DICT={'KEY1': {'KEY2': 'postgres'}}` if it doesn't exist and will add a field in existing dictionary by aforementioned route\n* `DD_A__B_LIST_APPEND_JSON=[1, 2, 3, [1, 2, \\\"3\\\"]]` will append `[1, 2, 3, [1, 2, \"3\"]]` to the end of value `V` from `A={\"B\": V}`\n\n## How to use\n\n```python\nfrom env2dict import parse_vars\n\nnew_vars = parse_vars(\n    prefix='DD_',\n    initial_vars=None,\n    source=None\n)\n```\n\n## More info\n\nPlease take a look at: \n* `parse_vars` function docstring\n* `tests` directory of this package repo.\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Environment to python dictionary parser util",
    "version": "0.0.3",
    "project_urls": {
        "Homepage": "https://github.com/PasaOpasen/py-env-parser"
    },
    "split_keywords": [
        "env",
        "environment"
    ],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "b4ee673dc94765c3232bcf94c8d4c3c947e06661bda1855a4b4b40b77e2b7056",
                "md5": "105387c9cbd763396fa54e20d60e8b75",
                "sha256": "dab3e67340ada52f1607d7f9477937493b272f1ac4a0db6d96efcdb9dbcf0416"
            },
            "downloads": -1,
            "filename": "env2dict-0.0.3-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "105387c9cbd763396fa54e20d60e8b75",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": null,
            "size": 6131,
            "upload_time": "2024-01-06T11:45:41",
            "upload_time_iso_8601": "2024-01-06T11:45:41.295147Z",
            "url": "https://files.pythonhosted.org/packages/b4/ee/673dc94765c3232bcf94c8d4c3c947e06661bda1855a4b4b40b77e2b7056/env2dict-0.0.3-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "53cba2b8fa098660f51750459e39471da68e9ae221a8f475d78c035c7801919e",
                "md5": "649f1cfd031ac82ded3068646b4ee161",
                "sha256": "c5ba4ef357932a85827b7e2aa9b2c49d3534168947ea36997e31a709ce872a46"
            },
            "downloads": -1,
            "filename": "env2dict-0.0.3.tar.gz",
            "has_sig": false,
            "md5_digest": "649f1cfd031ac82ded3068646b4ee161",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": null,
            "size": 5686,
            "upload_time": "2024-01-06T11:45:42",
            "upload_time_iso_8601": "2024-01-06T11:45:42.606284Z",
            "url": "https://files.pythonhosted.org/packages/53/cb/a2b8fa098660f51750459e39471da68e9ae221a8f475d78c035c7801919e/env2dict-0.0.3.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-01-06 11:45:42",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "PasaOpasen",
    "github_project": "py-env-parser",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": false,
    "requirements": [],
    "lcname": "env2dict"
}
        
Elapsed time: 0.16871s