将文件节配置为对象

fileconfig的Python项目详细描述


Latest PyPI VersionLicenseSupported Python VersionsFormat

TravisCodecov

file config将配置文件节转换为类的实例。创建 类引用ini文件 要创建的实例。以节名称作为参数调用类 将返回具有给定节中指定参数的实例。

安装

这个包在python 2.7和3.5+下运行,使用pip安装:

$ pip install fileconfig

用法

创建为fileconfig.Config的子类并设置其filename属性 到ini文件的路径。

如果文件名是相对的,则将其解析为相对于 定义类的模块(即相对于当前工作的not) 目录,如果它的文件不在那里)。

>>>importfileconfig>>>classCfg(fileconfig.Config):...filename='docs/pet-shop.ini'...def__init__(self,key,**kwargs):...self.key=key...self.__dict__.update(kwargs)

在创建实例时,将使用节调用__init__方法 名称(key)和 指定的文件。

假设您的ini文件是这样开始的:

[parrot]species=Norwegian bluecan_talk=yesquantity=0characteristics=beautiful plumage, pining for the fjords

要检索此实例,请使用其节名称调用类。

>>>c=Cfg('parrot')>>>print(c){'can_talk':'yes','characteristics':'beautiful plumage, pining for the fjords','key':'parrot','quantity':'0','species':'Norwegian blue'}

单重态

只会为每个配置文件创建、缓存和返回one实例 部分(也称为单重态模式):

>>>Cfg('parrot')iscTrue

构造函数也是idempotent

>>>Cfg(c)iscTrue

默认的__repr__实例允许往返:

>>>c__main__.Cfg('parrot')

别名

可以为每个节指定一个空格分隔的aliases列表:

[slug]aliases=snail special_offerspecies=slugcan_talk=noquantity=1

有关更改分隔符的信息,请参见下文。

别名映射到same实例:

>>>s=Cfg('special_offer')>>>s__main__.Cfg('slug')>>>sisCfg('snail')isCfg('slug')True

检查实例names(键+别名):

>>>s.key'slug'>>>s.aliases['snail','special_offer']>>>s.names['slug','snail','special_offer']

继承

配置文件节可以从另一节继承:

[Polly]inherits=parrotcan_talk=nocharacteristics=dead, totally stiff, ceased to exist

指定的键覆盖继承的键:

>>>print(Cfg('Polly')){'can_talk':'no','characteristics':'dead, totally stiff, ceased to exist','inherits':'parrot','key':'Polly','quantity':'0','species':'Norwegian blue'}

节可以从单个节继承。多重继承或传递继承 不支持。

自省

使用该类遍历所有部分中的实例:

>>>list(Cfg)[__main__.Cfg('parrot'),__main__.Cfg('slug'),__main__.Cfg('Polly')]

打印所有实例的字符串表示:

>>>Cfg.pprint_all()# doctest: +ELLIPSIS{'can_talk':'yes','characteristics':'beautiful plumage, pining for the fjords','key':'parrot',...

提示

除了keyaliasesinherits参数之外,您的 __init__方法从配置文件接收未处理的字符串。 分析器。

使用__init__方法处理其他参数以满足您的需要。

>>>classPet(Cfg):...def__init__(self,can_talk,quantity,characteristics=None,**kwargs):...self.can_talk={'yes':True,'no':False}[can_talk]...self.quantity=int(quantity)...ifcharacteristicsisnotNoneandcharacteristics.strip():...self.characteristics=[c.strip()forcincharacteristics.split(',')]...super(Pet,self).__init__(**kwargs)>>>print(Pet('Polly')){'can_talk':False,'characteristics':['dead','totally stiff','ceased to exist'],'inherits':'parrot','key':'Polly','quantity':0,'species':'Norwegian blue'}

这样,__init__方法还根据需要定义参数,或者 可选,设置默认值等。

覆盖

有时需要组合多个配置文件,例如使用默认值 包目录中包含的文件,由 不同的地点。

要支持此操作,子类fileconfig.Stacked并将filename设置为 默认配置的位置。

>>>classSettings(fileconfig.Stacked):...filename='docs/pet-shop.ini'

使用add方法加载覆盖配置文件:

>>>Settings.add('docs/lumberjack.ini')

如果文件名是相对的,则将其解析为相对于 调用add方法的模块

您可以访问所有文件中的部分:

>>>print(Settings('Bevis')){'can_talk':'yes','characteristics':"sleeps all night, works all day, puts on women's clothing",'key':'Bevis','species':'human'}

只要他们有不同的名字:

>>>print(Settings('Polly')){'can_talk':'no','characteristics':'dead, totally stiff, ceased to exist','inherits':'parrot','key':'Polly','quantity':'0','species':'Norwegian blue'}

使用相同名称添加到堆栈掩码部分顶部的配置文件 从以前的文件:

>>>print(Settings('parrot')){'characteristics':'unsolved problem','key':'parrot'}

定制

aliases使用不同的分隔符覆盖_split_aliases 你班上的方法。将其设为staticmethodclassmethod,需要 字符串参数并返回已拆分的列表。

默认情况下,fileconfig将使用^{TT22}$来自 分析配置文件的标准库。要使用不同的解析器, 重写fileconfig.Config子类中的_parser属性。

指定编码 配置解析器,重写子类的_encoding属性。

如果找不到配置文件,file config将引发错误。如果你想要这个 error若要以静默方式传递,请在 子类到True

潜在问题

这个包使用sys._getframe(与 inspect.currentframeseedocs)。在ironpython下,这可能需要 启用解释器的FullFrames选项。

许可证

fileconfig分布在MIT license下。

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
java如何在数据库中插入行时自动发送电子邮件?   从进程输出读取的Java问题   java质疑StyledDocument和JTextPane之间的关系,以及接口的正确使用   java错误getPlayer(args[0]);   java如何使Spring引导在重新打包的WAR中包含清单文件?   Java中的除法与模   java使用2d数组和JfreeChart制作散点图   java扩展SonarQube FindBugs插件和自定义FindBugs插件   javaspring:hibernate+ehcache   具有不正确的equals和HashCode实现的java HashMap   java Jaspersoft报告网。旧金山。jasperreports。发动机例外:net。旧金山。jasperreports。发动机填满JRepressionEvalException:计算表达式时出错   java如果输入与其变量不匹配,如何添加错误   在java中使用简单数组[]实现队列   无法启动上下文路径/hsx上的java FAIL应用程序