Package: py-gstools
PY-GSTOOLSTool classes and functions for Guiosoft projectsInstallingpipinstallpy-gstoolsCacheCache wrapper for multiple providers.Usage:fromdatetimeimporttimedeltafromgs.cacheimportget_cachecache=get_cache('memory')#    connection string can be:#    - memory#    - path:/path/to/cache/directory#    - redis://host:port/db_numbercache.set(key='key',value='This is an cached data',ttl=timedelta(seconds=600))value=cache.get('key')print(value)DotEnvRead environment variables from .env file(Yeah, I know there is a py-dotenv package, but this is small and better to my needs)Usage:importosfromgs.dotenvimportload_env# .env file# CONFIGURATION_ONE=some_nasty_thing# LOG_LEVEL=debugload_env(verbose=True)2022-06-2815:59:05,052INFOload_env(file_name=.env-{'CONFIGURATION_ONE':'some_nasty_thing','LOG_LEVEL':'debug'})ConfigConfiguration classes from files or environment variablesRead data from environment variablesfromgs.configimportBaseConfigclassEnvConfig(BaseConfig):"""Environment based config"""TESTING_ALPHA:str='alpha'# ENV:TEST_ALPHATESTING_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_GAMMAcfg=EnvConfig.load_from_env()print(cfg.sample_dict()){'TESTING_BETA':'beta','TEST_ALPHA':'alpha','TEST_GAMMA':False}Read data from fileconfig.json file{"TESTING_BETA":"beta","TEST_ALPHA":"alpha","TEST_GAMMA":true}config.yaml fileTESTING_BETA:betaTEST_ALPHA:alphaTEST_GAMMA:truefromgs.configimportBaseConfigclassEnvConfig(BaseConfig):"""Environment based config"""TESTING_ALPHA:str='alpha'# ENV:TEST_ALPHATESTING_BETA:str='beta'TESTING_GAMMA:bool=True# ENV:TEST_GAMMAcfg=EnvConfig.load_from_file('config.json')print(cfg.sample_dict()){'TESTING_BETA':'beta','TEST_ALPHA':'alpha','TEST_GAMMA':True}Composite configurationsconfig.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 fileINT_ARG:2INT_ARG_2:10LIST_ARG:-'1'-'2'-'3'-'4'STR_ARG:1234ABCDSUB_CONFIG:ARG_1:10ARG_2:ABCDSUB_CONFIGS:-ARG_1:2ARG_2:EFGH-ARG_1:3ARG_2:IJKLfromgs.configimportBaseConfigclassSubConfig(BaseConfig):"""Sample configuration class"""ARG_1:int=10ARG_2:str='abc'classConfig(BaseConfig):"""Sample configuration class"""INT_ARG:int=1# DOCUMENT FOR INT_ARGINT_ARG_2:intSTR_ARG='abcd'# DOCUMENT FOR STR_ARGLIST_ARG:List[str]=['a','b','c','d']SUB_CONFIG:SubConfigSUB_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 validationfromgs.configimportBaseConfigclassConfig(BaseConfig):ARG_1:int=10ARG_2:str='abc'defafter_load(self):ifself.ARG_1<=0:# Invalid argumentraiseTypeError('ARG_1 must be positive',self.ARG_1)ifself.ARG_2=='ABC':# Fixing argumentself.ARG_2='abc'# Add anything you need to do/validate after loading