Package: configy
Simple Configuration manager, plays well with testing.Basic UsageInstall from pypi:pipinstallconfigySpecify the configuration directives as early in execution as possible:importconfigytry:# Every option is optional, fill in as makes sense.configy.load_config(conf='the_configuration.yaml',# The default config file if not specified as an ENV varenv='CONFIGY_FILE',# The ENV var to look for a config filedefaults='defaults.yaml',# The defaults that is always loaded.data={'manual':'defaults'},# Manually provided defaults loadedcase_sensitive=True# Case Sensitive by default)exceptconfigy.ConfigyErrorase:# Report config load error to userGiven a sample YAML config file of:Something:value:The Valuenumber:42bool1:1bool2:FALSEbool3:yYou then use it so:>>>fromconfigyimportconfig>>>config.Something.value'The Value'If you try to access any configuration value that isn’t defined you will get an exception:>>>config.Something.otherTraceback(mostrecentcalllast):File"<stdin>",line1,in<module>KeyError:'other'The config object is just a dictionary, so you can use it as a regular dictionary as well:>>>config['Something']['value']'The Value'>>>config.Something.get('other','default value')'default value'Environment variable substitutionYou can request Environment variable substitution by using${ENVVAR}in the yaml document:Something:full_value:${ENV_FULL}part_value:http://${ENV_PART}/somethingIf the envvar isn’t defined, it wills raiseConfigyError.Helper functionsSince you can’t guarantee the type of a value in the configuration files (YAML treats everything as text), you need to do type conversion manually.For ints and floats it is easy:>>>int(config.Something.number)42>>float(config.Something.number)42.0For booleans it is a bit more tricky, as a boolean can be represented by many different notations. You also don’t have complete control over the notation used. For this we provide ato_bool()helper function.It treats case-insensitivelyTrue‘y’, ‘yes’, ‘1’, ‘t’,’true’False‘n’, ‘no’, ‘0’, ‘f’, ‘false’Anything else will resort to the provided default (which defaults to False)>>>fromconfigyimportconfig,to_bool>>>to_bool(config.Something.bool1)True>>>to_bool(config.Something.bool2)False>>>to_bool(config.Something.bool1)True>>>to_bool(config.Something.number)False>>>to_bool(config.Something.number,True)True>>>to_bool(config.Something.number,None)NoneHow to overload settings for testingDuring testing, one often wants to override some configuration to test something specific.
Configy supports this use case.fromconfigyimportconfig,testconfig@testconfig.override_config({'Something':{'other':'I now exist',},'Extra':'defined',})deftest_override():# Existing values still work as per usualassertconfig.Something.value=='The Value'# New valuesassertconfig.Something.other=='I now exist'assertconfig.Extra=='defined'One can also define configuration to be used:@testconfig.load_config(conf='test_config.yaml')deftest_load_config():assertconfig.testvalue=='test result'You can also define the WHOLE configuration that is loaded for that test:@testconfig.load_config(data={'testvalue':'test result',})deftest_load_config_data():assertconfig.testvalue=='test result'All the testing decorators will work on method, class and function level.