一个python模块,可以方便地读取和写入yaml配置文件。
yaml-configuration的Python项目详细描述
Yaml_配置
此模块为其他模块或启动提供了简单的配置 剧本。它提供了加载和保存配置文件以及读取 并向其写入配置值。同时尝试通过 如果实际配置值不是 支持present。
用法
类defaultconfig提供包的基本功能。 自定义配置类就是从它派生出来的。
importosimportloggingfrompytestimportraisesfromyaml_configuration.configimportDefaultConfig,ConfigErrordefread_file(file_path,filename):file_path=os.path.join(file_path,filename)withopen(file_path,'r')asfile_pointer:file_content=file_pointer.read()returnfile_contentclassBasicConfig(DefaultConfig):def__init__(self,config_string,config_file,logger_object=None):super(BasicConfig,self).__init__(config_string,logger_object)# this is already done in the init# self.load(config_file, path=os.path.dirname(__file__))if__name__=='__main__':config_file="basic_config.yaml"config_string=read_file(os.path.dirname(__file__),config_file)basic_config=BasicConfig(config_string,config_file,logging.getLogger("TestLogger"))basic_config.set_config_value("number_value",10)basic_config.set_config_value("string_value","test_string")assertbasic_config.get_config_value("string_value")=="test_string"assertbasic_config.get_config_value("not_existing_config_value",default=42)==42withraises(ConfigError):ifnotbasic_config.get_config_value("value_that_should_exist"):raiseConfigError("The config value with key 'value_that_should_exist' should exist")