Skip to main content

A python library for helper functions

Project description

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 _FILE companion (file-based secrets).
  • short_msg: Truncates the message to chars characters and adds two dots at the end.
  • strtobool: Converts a string to a boolean
  • redact: Replaces in message the param string with replace_value

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)

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")

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

egos_helpers-1.3.1.tar.gz (9.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

egos_helpers-1.3.1-py3-none-any.whl (10.4 kB view details)

Uploaded Python 3

File details

Details for the file egos_helpers-1.3.1.tar.gz.

File metadata

  • Download URL: egos_helpers-1.3.1.tar.gz
  • Upload date:
  • Size: 9.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.3 CPython/3.14.5 Linux/7.0.0-28-generic

File hashes

Hashes for egos_helpers-1.3.1.tar.gz
Algorithm Hash digest
SHA256 69431d520c63c5d2bec2ec3c72fb2450ada2484893d987f31ec4cf53c662ac85
MD5 51f530fcce30ab6f5c2c198194947a43
BLAKE2b-256 fa20006a4d14f45699bbdd41a13b8aa54c0c8eeea97c98000a009392050eeb67

See more details on using hashes here.

File details

Details for the file egos_helpers-1.3.1-py3-none-any.whl.

File metadata

  • Download URL: egos_helpers-1.3.1-py3-none-any.whl
  • Upload date:
  • Size: 10.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/2.3.3 CPython/3.14.5 Linux/7.0.0-28-generic

File hashes

Hashes for egos_helpers-1.3.1-py3-none-any.whl
Algorithm Hash digest
SHA256 eda22b189fd5946ae8bcdb9b7499c585cba58f433e2f35907c4b8f8557c05ba3
MD5 a75c3439b3b6af341fb1f52fb036b9e6
BLAKE2b-256 fae45d165eabf8ecca20b671e5e265c18ad816934f789b696e396f6d9da6e47f

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page