实现指定类型返回值的os.getenv包装器
smart-getenv的Python项目详细描述
由于os.environ中的环境变量是字符串,因此通常不方便存储和检索其他 数据类型,如bool或list。该包提供了一个包装os.getenv的函数,并允许 指定所需的变量类型。
测试和支持的类型:str、int、float、list。元组,dict.
用法
安装:
$ pip install smart-getenv
准备变量:
$ exportBOOLEAN=true $ exportLIST=a,b,c $ exportTRICKY_LIST=d:e:f $ exportDICT="{'foo':'bar'}"
抓住他们:
>>>fromsmart_getenvimportgetenv>>>>>>getenv('BOOLEAN',type=str)'true'>>>getenv('BOOLEAN',type=bool)True>>>getenv('LIST',type=list)['a','b','c']>>>getenv('LIST',type=tuple)('a','b','c')>>>getenv('TRICKY_LIST',type=list,separator=':')['d','e','f']>>>getenv('DICT',type=dict){'foo':'bar'}>>>getenv('LOST',default='default value anyone?')'default value anyone?'
运行测试:
$ python tests.py