egos-helpers
A python library for helper functions. Used in the egos-tech projects.
Functions
gather_environ: Return a dict of environment variables correlating to the keys dict.get_environ_value: Resolve a single value from an environment variable or its_FILEcompanion (file-based secrets).short_msg: Truncates the message tocharscharacters and adds two dots at the end.strtobool: Converts a string to a booleanredact: Replaces inmessagetheparamstring withreplace_valuekey_to_title: Converts a key in the forma_is_bto a title in the formA Is Bslugify: Converts a string to a URL-safe slug of at mostmax_lengthcharacterssetup_logger: Sets up and returns a logger, optionally with a GELF handlerget_formatter: Builds the log record formatter, which adds source locations atDEBUG
See egos_helpers/core.py for the full description of each function.
Usage example
Install egos-helpers:
python3 -m venv .venv
source .venv/bin/activate
# if you don't have a requirements.txt file
pip install -U egos-helpers
# if you do have a requirements.txt file
pip install -U -r requirements.txt
In your python project:
from egos_helpers import gather_environ
class MyClass:
keys = {
"key_one": {
"default": ["one", 2],
"type": "list",
},
"key_two": {
"hidden": True,
"default": False,
"type": "boolean",
},
"key_three": {
"default": {},
"type": "filter",
},
"key_four": {
"default": None,
"redact": True,
"type": "string",
},
"key_five": {
"default": 12,
"type": "int",
},
"key_six": {
"default": "INFO",
"type": "enum",
"values": ["DEBUG", "INFO", "WARNING", "ERROR"],
},
"key_seven": {
"default": "12",
"deprecated": True,
"replaced_by": "key_five",
"type": "int",
},
}
def __init__(self):
envkeys = gather_environ(keys=MyClass.keys)
print(envkeys)
With KEY_ONE=one, KEY_THREE=foo=bar, KEY_FOUR=super_secret_string, KEY_FIVE=33,
KEY_SIX=WARNING and the rest unset, gather_environ returns:
{
"key_one": ["one"],
"key_two": False,
"key_three": {"foo": "bar"},
"key_four": "super_secret_string",
"key_five": 33,
"key_six": "WARNING",
}
key_seven is deprecated and unset, so it is left out. A key that is unset returns its
default exactly as declared, without being converted to the key's type.
File-based secrets
Use get_environ_value to resolve a value from either an environment variable or a
<NAME>_FILE file path (the container secrets convention). It reads <NAME>_FILE
first (returning the file contents stripped of surrounding whitespace), falls back
to <NAME>, and returns None when neither is set. Setting both raises an error.
So does an unreadable <NAME>_FILE path.
from egos_helpers import get_environ_value
# MYAPP_TOKEN=secret123 -> "secret123"
# MYAPP_TOKEN_FILE=/run/secrets/t -> contents of the file, stripped
token = get_environ_value("MYAPP_TOKEN")
Metadata
Release files for egos-helpers 1.3.4
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| egos_helpers-1.3.4.tar.gz | 12.1 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| egos_helpers-1.3.4-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 24.1 kB
Release files / egos_helpers-1.3.4.tar.gz
| Download URL | egos_helpers-1.3.4.tar.gz |
|---|---|
| Size | 12.1 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
17779624b8c50506c38a05c33dfdf1ae8e1fafc6897413caf1c73281e45239a4
|
|
BLAKE2b-256 checksum How to use checksums |
827aaec450ce34e5d0bfd2262d29e00de1b62b1982ee2318af0b8b66419ad3e2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.3.3 CPython/3.14.5 Linux/7.0.0-28-generic
|
Release files / egos_helpers-1.3.4-py3-none-any.whl
| Download URL | egos_helpers-1.3.4-py3-none-any.whl |
|---|---|
| Size | 12.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
d94fdcf3ed9df2cc305df90a59acc2023e665eb5204d9a2afa7eab4a942a86fc
|
|
BLAKE2b-256 checksum How to use checksums |
6c46a061b9c562779dad7229b046dc19cc538befb85aba1d9e1af446f91db927
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
poetry/2.3.3 CPython/3.14.5 Linux/7.0.0-28-generic
|