Tool classes and functions for Guiosoft projects
Project description
PY-GSTOOLS
Tool classes and functions for Guiosoft projects
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
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file py-gstools-0.2.1.tar.gz.
File metadata
- Download URL: py-gstools-0.2.1.tar.gz
- Upload date:
- Size: 13.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
de1c01aa0514680d4872b6e25006471681c4cec2ef94d8a197db88328df447d1
|
|
| MD5 |
516f4bb54a9d01871d8905c7c6399044
|
|
| BLAKE2b-256 |
d7fe680a0f222f123ca914b237b5a722c54009e789ca65afc3344cde227d4aff
|
File details
Details for the file py_gstools-0.2.1-py3-none-any.whl.
File metadata
- Download URL: py_gstools-0.2.1-py3-none-any.whl
- Upload date:
- Size: 11.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.1 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1479309e479676e5fa5fdbe9a6b070006f18c6529f9d70495242f433defe1710
|
|
| MD5 |
bfbcc2bfd7915c3577c9044a1613e27a
|
|
| BLAKE2b-256 |
d2a99813410f8e53e9d6847e1f31e30e5a36badc389a3c3db20dd575d2918761
|