Skip to main content

PY-GSTOOLS

Tool classes and functions for Guiosoft projects

CodeQL Upload Python Package PyPI PyPI - Downloads Pylint codecov

Installing

pip install py-gstools

Cache

Cache wrapper for multiple providers.

Usage:

from datetime import timedelta
from gs.cache import get_cache

cache = get_cache('memory')

#    connection string can be:
#    - memory
#    - path:/path/to/cache/directory
#    - redis://host:port/db_number

cache.set(key='key',
          value='This is an cached data', 
          ttl=timedelta(seconds=600))

value = cache.get('key')

print(value)

DotEnv

Read environment variables from .env file

(Yeah, I know there is a py-dotenv package, but this is small and better to my needs)

Usage:

import os
from gs.dotenv import load_env

# .env file
# CONFIGURATION_ONE=some_nasty_thing
# LOG_LEVEL=debug

load_env(verbose=True)
2022-06-28 15:59:05,052 INFO load_env(file_name=.env - {'CONFIGURATION_ONE': 'some_nasty_thing', 'LOG_LEVEL': 'debug'})

Config

Configuration classes from files or environment variables

Read data from environment variables

from gs.config import BaseConfig

class EnvConfig(BaseConfig):
    """Environment based config"""

    TESTING_ALPHA: str = 'alpha'   # ENV:TEST_ALPHA
    TESTING_BETA: str = 'beta'
    TESTING_GAMMA: bool = False     # ENV:TEST_GAMMA

# Here, the environment variables must be available by the OS (by os.environ)

# TESTING_ALPHA field maps to TEST_ALPHA environment var (defined in the comment)
# TESTING_BETA field maps to TESTING_BETA (default behavior, same to field name)
# TESTING_GAMMA fiels maps to TEST_GAMMA

cfg = EnvConfig.load_from_env()

print(cfg.sample_dict())

{'TESTING_BETA': 'beta', 'TEST_ALPHA': 'alpha', 'TEST_GAMMA': False}

Read data from file

config.json file

{
"TESTING_BETA": "beta", 
"TEST_ALPHA": "alpha",
"TEST_GAMMA": true
}

config.yaml file

TESTING_BETA: beta
TEST_ALPHA: alpha
TEST_GAMMA: true
from gs.config import BaseConfig

class EnvConfig(BaseConfig):
    """Environment based config"""

    TESTING_ALPHA: str = 'alpha'   # ENV:TEST_ALPHA
    TESTING_BETA: str = 'beta'
    TESTING_GAMMA: bool = True     # ENV:TEST_GAMMA

cfg = EnvConfig.load_from_file('config.json')

print(cfg.sample_dict())

{'TESTING_BETA': 'beta', 'TEST_ALPHA': 'alpha', 'TEST_GAMMA': True}

Composite configurations

config.json file

{
    "INT_ARG": 2,
    "LIST_ARG": ["1", "2", "3", "4"],
    "STR_ARG": "1234ABCD",
    "SUB_CONFIG": {
        "ARG_1": 10, 
        "ARG_2": "abc"
        },
    "INT_ARG_2": 10,
    "SUB_CONFIGS": [
        {
            "ARG_1": 2, 
            "ARG_2": "EFGH"
        },
        {
            "ARG_1": 3,
            "ARG_2": "IJKL"
        }
    ]
}

config.yaml file

INT_ARG: 2
INT_ARG_2: 10
LIST_ARG:
- '1'
- '2'
- '3'
- '4'
STR_ARG: 1234ABCD
SUB_CONFIG:
  ARG_1: 10
  ARG_2: ABCD
SUB_CONFIGS:
- ARG_1: 2
  ARG_2: EFGH
- ARG_1: 3
  ARG_2: IJKL
from gs.config import BaseConfig

class SubConfig(BaseConfig):
    """Sample configuration class"""
    ARG_1: int = 10
    ARG_2: str = 'abc'


class Config(BaseConfig):
    """Sample configuration class"""

    INT_ARG: int = 1  # DOCUMENT FOR INT_ARG
    INT_ARG_2: int
    STR_ARG = 'abcd'  # DOCUMENT FOR STR_ARG
    LIST_ARG: List[str] = ['a', 'b', 'c', 'd']
    SUB_CONFIG: SubConfig
    SUB_CONFIGS: List[SubConfig]

cfg = EnvConfig.load_from_file('config.json')

print(cfg.to_dict())

{'LIST_ARG': ['1', '2', '3', '4'], 'INT_ARG': 2, 'STR_ARG': '1234ABCD', 'SUB_CONFIG': {'ARG_2': 'abc', 'ARG_1': 2}, 'INT_ARG_2': 10, 'SUB_CONFIGS': [{'ARG_2': 'EFGH', 'ARG_1': 2}, {'ARG_2': 'IJKL', 'ARG_1': 3}]}

After loading validation

from gs.config import BaseConfig

class Config(BaseConfig):
    
    ARG_1: int = 10
    ARG_2: str = 'abc'

    def after_load(self):
        if self.ARG_1 <= 0:
            # Invalid argument
            raise TypeError('ARG_1 must be positive', self.ARG_1)
        if self.ARG_2 == 'ABC':
            # Fixing argument
            self.ARG_2 = 'abc'
        
        # Add anything you need to do/validate after loading

Metadata

Release files for py-gstools 0.2.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for py-gstools 0.2.1
File Size Uploaded Provenance Actions
py-gstools-0.2.1.tar.gz 13.1 kB Without provenance Details for py-gstools-0.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for py-gstools 0.2.1
File Interpreter ABI Platform Actions
py_gstools-0.2.1-py3-none-any.whl Python 3 none any Details for py_gstools-0.2.1-py3-none-any.whl

Total release size: 24.5 kB

Release files / py-gstools-0.2.1.tar.gz

Download URL py-gstools-0.2.1.tar.gz
Size 13.1 kB
Tags Source
SHA-256 checksum
How to use checksums
de1c01aa0514680d4872b6e25006471681c4cec2ef94d8a197db88328df447d1
BLAKE2b-256 checksum
How to use checksums
d7fe680a0f222f123ca914b237b5a722c54009e789ca65afc3344cde227d4aff
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / py_gstools-0.2.1-py3-none-any.whl

Download URL py_gstools-0.2.1-py3-none-any.whl
Size 11.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1479309e479676e5fa5fdbe9a6b070006f18c6529f9d70495242f433defe1710
BLAKE2b-256 checksum
How to use checksums
d2a99813410f8e53e9d6847e1f31e30e5a36badc389a3c3db20dd575d2918761
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Security for py-gstools 0.2.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page