Package: python-config-parser
python-config-parserpython-config-parser lets you create runtime configuration objects using json or yaml files.MAIN FEATURESDeclarative configurations without using .ini filesAccess using OOP or subscriptable, which means that you can iterate the config object itemsRuntime validation usingschemaAutomatic environment variables interpolationAutomatic parser selecting using config file extensionHOW TO INSTALLUsepipto install it.pipinstallpython-config-parserHOW TO USEBy default, the config file will look for any of the following config files in theconfigdirectory:config.json/config.yaml/config.yml.
You can change the config directory and or config file according to your preference (assuming your current directory).frompyconfigparserimportconfigparserconfigparser.get_config(CONFIG_SCHEMA,config_dir='your_config_dir_path',file_name='your_config_file_name')Schema validationYou may or not use schema validation. If you want to use it, it will validate and apply rules to the whole config object before returning it.
If you choose to not use it, it won't validate the config object before returning it, and it may generate runtime access inconsistencies.
How to use schemafromschemaimportUse,AndSCHEMA_CONFIG={'core':{'logging':{'format':And(Use(str),lambdastring:len(string)>0),'date_fmt':And(Use(str),lambdastring:len(string)>0),'random_env_variable':str},'allowed_clients':[{'ip':str,# <- Here you can use regex to validate the ip format'timeout':int}]}}Theconfig.ymlfilecore:random_env_variable:${RANDOM_ENV_VARIABLE}logging:format:"[%(asctime)s][%(levelname)s]:%(message)s"date_fmt:"%d-%b-%y%H:%M:%S"allowed_clients:-ip:192.168.0.10timeout:60-ip:192.168.0.11timeout:100A json config file would be something like:{"core":{"random_env_variable":"${RANDOM_ENV_VARIABLE}","logging":{"format":"[%(asctime)s][%(levelname)s]: %(message)s","date_fmt":"%d-%b-%y %H:%M:%S"},"allowed_clients":[{"ip":"192.168.0.10","timeout":60},{"ip":"192.168.0.11","timeout":100}]}}The config instancefrompyconfigparserimportconfigparser,ConfigErrorimportloggingtry:config=configparser.get_config(SCHEMA_CONFIG)# <- Here I'm using that SCHEMA_CONFIG we've already declaredexceptConfigErrorase:print(e)exit()# to access your config you just need to:fmt=config.core.logging.format#at this point I'm already using the config variablesdate_fmt=config['core']['logging']['date_fmt']# here subscriptable accesslogging.getLogger(__name__)logging.basicConfig(format=fmt,datefmt=date_fmt,level=logging.INFO)# the list of object example:forclientinconfig.core.allowed_clients:print(client.ip)print(client.timeout)# You can also iterate objects, but instead of giving the property it'll give you the property's name# And then you can access the values by subscriptale accessforlogging_section_attr_keyinconfig.core.logging:print(config.core.logging[logging_section_attr_key])# Accessing the environment variable already resolvedprint(config.random_env_variable)Assuming you've already created the first Config's instance this instance will be cached inside Config class,
so after this first creation you just need to  re-invoke Config.get_config() without any argumentfrompyconfigparserimportconfigparserconfig=configparser.get_config()You can also disable this caching behaviorfrompyconfigparserimportconfigparserconfigparser.hold_an_instance=FalseEnvironment Variables InterpolationIf the process does not find a value already set to your env variables
It will raise a ConfigError. But you can disable this behavior, and the parser will setNoneto these unresolved env varsfrompyconfigparserimportconfigparserconfigparser.ignore_unset_env_vars=Trueconfig=configparser.get_config()CONTRIBUTEForkhttps://github.com/BrunoSilvaAndrade/python-config-parser/, create commit and pull request todevelop.