通过配置文件公开关键字参数的decorator库
configf的Python项目详细描述
配置
decorator库通过配置文件公开关键字参数
configfy是一个原型库,它允许向用户快速公开函数参数,以便修改程序行为,而无需处理参数解析或控制通过程序的配置选项流。它被实现为一个使用python configparser模块的decorator库。
它可以用来快速地向函数添加新参数, 或者用完整的配置管理代替应用程序。
旅馆在github
安装
configfy最好使用pip安装
pip install configfy
简单示例
定义一个使用关键字参数并覆盖默认值的函数 具有配置文件设置的关键字参数。
importconfigfyfromconfigfyimportconfigfyascfy@cfydefhello(name,another_name='Pedro'):"""Be nice and say hello """print(f'Hello {name}, I am {another_name}!')hello('Bob')
包含
[global]another_name='Suzan Flusan'
产生
Hello Bob, I am Suzan Flusan!
注意:请注意,库希望存在至少包含空部分[全局]的配置文件(./configfy.ini)。
高级示例
演示如何指定要在配置文件中使用的节,以及如何在运行时更改配置文件或在装饰器中指定配置文件。
From docs/example.py
importconfigfyfromconfigfyimportconfigfyascfy@cfydefhello(name,another_name='Pedro'):"""Be nice and say hello """print('Hello %s, I am %s!'%(name,another_name))@cfy(section='greetings_section')defgreetings(name,language='english'):"""Give a nice greeting ... """iflanguage=='english':print('Hello %s! How are you doing?'%(name))eliflanguage=='spanish':print('Hola %s! Que tal?'%(name))eliflanguage=='german':print('Hallo %s! Wie gehts?'%(name))eliflanguage=='serbian':print('Zdravo %s! Kako si?'%(name))else:print('!nuqneH %s!'%(name))@cfy(config='yet_another_config.ini')defgoodby(msg='Goodby!'):print(msg)print('# Use default configfy.ini file (missing greetings section)...')print('# Current config: %s'%configfy.configfile.config)hello('Bob')greetings('Tom')goodby()print('\n# Changing config to "another_config.ini" ...')configfy.set_active_config_file('another_config.ini')print('After setting new config file, current config: %s'%configfy.configfile.config)hello('Bob')greetings('Tom')goodby()print('\n# Specifying kwargs, overwriting config settings...')hello('Bob',another_name='Alfredo')greetings('Tom',language='serbian')goodby(msg='That\'s all Folks!')
产生
# Use default configfy.ini file (missing greetings section)...# Current config: OrderedDict([('global', {'another_name': 'Suzan Flusan', 'language': 'spanish'})]) Hello Bob, I am Suzan Flusan! # 2018-08-01 17:45:53,834 - configfy - WARNING - Config section greetings_section not found! Hello Tom! How are you doing? Goodby! # Changing config to "another_config.ini" ... After setting new config file, current config: OrderedDict([('global', {'language': 'spanish', 'msg': 'A goodby message! Ignored'}), ('greetings_section', {'language': 'german'})]) Hello Bob, I am Pedro! Hallo Tom! Wie gehts? Goodby! # Specifying kwargs, overwriting config settings... Hello Bob, I am Alfredo! Zdravo Tom! Kako si? That's all Folks!
完成配置替换
这是一个使用configfy作为通用工具替换 应用程序的配置文件。源代码可以在./doc中找到
importclickimportconfigfyfromconfigfyimportconfigfyascfy# Two user functions that get their kwargs from the current config file@cfydefuser_function1(kw1=23,name1=''):print('Calling user_function1(%s, %s) ...'%(kw1,name1))@cfydefuser_function2(kw2=None,name2=''):print('Calling user_function2(%s, %s) ...'%(kw2,name2))# Use the excellent click library to handle program arguments@click.group()@click.version_option(0.1)@click.option('-v','--verbose',count=True)@click.option('--config',default='configfy.ini',help='Specify the configfy file to use')defexample(verbose,config):""" Enabling sub commands """# Load config fileconfigfy.set_active_config_file(config)globalconfig_fileconfig_file=config@example.command()defex1():user_function1()@example.command()defex2():user_function2()if__name__=='__main__':example()
配置文件:
[global]# Pass a number and a listkw1=1001name1=['l', 'i', 's', 't']# Pass a dict and a setkw2={'d': 42}# Note: passing set(1, 2, 3) wont work. It will be passed as a stringname2={1, 2, 3}
用法
> python complete_example.py --config complete_example.ini ex1 Calling user_function1(1001, ['l', 'i', 's', 't']) ... > python complete_example.py --config complete_example.ini ex2 Calling user_function2({'d': 42}, {1, 2, 3}) ...
调试
使用configfy扩展的调试函数可能很棘手,因为 直接进入由configfy修改的用户函数,调试器将输入 首先配置库代码。
我们可以做的是让调试器“跳过”配置库代码。
PDB
importpdbfromconfigfyimportconfigfyascfy@cfydeffuu(kw_me=42):print(kw_me)if__name__=='__main__':# load pdb, making use of the skip parameterpdb.Pdb(skip=['configfy.*']).set_trace()fuu()
pudb
importpudbfromconfigfyimportconfigfyascfy# Prevent pudb from stepping into the decorator library codepudb._get_debugger(skip=['configfy.*'])@cfydeffuu(kw_me=42):print(kw_me)if__name__=='__main__':pudb.set_trace()fuu()
间接费用
你自己试试吧!运行
cd doc
python overhead.py
我得到
Comparing performance ... this can take a while. native: 11.863 sec configfy+config 11.953 sec configfy 12.160 sec
作者
我很高兴在github上或私下里收到任何反馈或评论manuel.pasieka@protonmail.ch
我为什么写这个模块
我编写这个库是因为我经常要在scientific设置中编写原型,在这个设置中,不清楚要实现什么目标。
我常常不得不在应用程序流的深层向函数引入额外的参数和选项,以便用户可以更改其行为。可以通过一些全局配置文件来实现,也可以解析程序参数并将它们通过多个层传递到所需的函数。
为了简化这些步骤并加快速度,我编写了这个库,它允许在代码库中的任何函数中添加关键字参数,并通过一个简单的配置文件向用户公开这些参数。
许可证
麻省理工学院