轻松保存和检索应用程序的设置。

EasySettings的Python项目详细描述


简易设置

easysettings允许您轻松地保存和检索简单的应用程序 设置。处理非字符串类型,如boolean、integer、long、list、as 以及正常的字符串设置。不需要节,只需set(),get(), 和save()。

还有其他的`*设置` 对象<;jsonsettings到lsettings和yamlsettings>;``允许您 使用标准格式,例如:

  • 已经 方法以json格式加载/保存配置。 加载json设置() 是加载配置文件的首选方法。
  • tomlsettings - userdict 的 方法以toml格式加载/保存配置。 加载toml设置() 是加载配置文件的首选方法。
  • 已经 方法以yaml格式加载/保存配置。 加载yaml设置() 是加载配置文件的首选方法。

错误修复

  • 3.2.0版:

添加了新的配置格式,如 jsonsettings 您现在可以使用 tomlsettings (通过 toml 包)和 yamlsettings (通过 pyyaml 包)。

应该可以使用 easysettings jsonsettings 没有这些新的依赖。只有当你想要 新的格式设置。

  • 3.0.0版:

可以在 jsonsettings 和settings/items可以在 解码/加载或在编码/保存之前。

这允许您以任何您认为合适的方式修改值 子类化。

示例

简单设置基本用法示例:

#!/usr/bin/env python# --------------- Creation ----------------fromeasysettingsimportEasySettingssettings=EasySettings("myconfigfile.conf")# configfile_exists() checks for existing config, and creates one if needed.# ** this function is called automatically now when a filename is passed to easysettings. **# if you wish to disable it, just do: settings = EasySettings() and set# settings.configfile later.# ------------- Basic Functions -----------# set without savingsettings.set("username","cjw")settings.set("firstrun",False)printsettings.get("username")# this results in "cjw"# check if file is savedifnotsettings.is_saved():print"you haven't saved the settings to disk yet."# ...settings are still available even if they haven't#    been saved to disk# savesettings.save()# you may also set & save in one line...settings.setsave("homedir","/myuserdir")

高级:

# check if setting exists if you wantifsettings.has_option('username'):print"Yes, settings has 'username'"# list settings/options/valuesmysettings=settings.list_settings()myoptions=settings.list_options()myvalues=settings.list_values()# remove settingsettings.remove('homedir')# clear all option names and valuessettings.clear()# clear all values, leave option names.settings.clear_values()

比较:

# compare two settings objectssettings2=EasySettings('myconfigfile2.conf')ifsettings.compare_opts(settings2):print"these have the same exact options, values may differ"ifsettings.compare_vals(settings2):print"these have the exact same values, options may differ"ifsettings==settings2:print"these have the exact same settings/values"# can also be written as settings.compare_settings(settings2)# if you like typing.. :)ifsettings>settings2:print"settings has more options than settings2"# all of them work ==, !=, <=, >= , > , <# ... the < > features are based on amount of options.#     the = features are based on option names and values.

功能

简单设置具有您期望设置之外的基本功能 模块,而且很容易使用。如果您的项目需要简单地保存 没有其他模块的开销和复杂性的设置 这是给你的。保存、加载、设置和获取非常容易掌握。更多 高级功能可以供您使用,但不要碍事。 可以列出、搜索、检测、删除设置、选项和值, &;已清除。

轻松设置在写入之前使用字典存储设置 磁盘,因此您还可以使用 简易设置。设置 。函数将保存 设置一个选项,并且 被保存的时间将告诉您是否 文件已保存到磁盘。代码是为新手编写的,所以 python控制台中的帮助('easysettings') 将帮助您入门。

list函数中的search_query参数用于查找设置, 按搜索字符串列出的选项和值:

mydebugoptions=settings.list_options('debug')# clear all debug values..settings.clear_values(mydebugoptions)

添加了非字符串类型,因此可以使用任何可以pickle的类型 作为期权的价值。这包括所有主要类型,比如int,long, float、boolean和list。所有这些值都将作为 设置的类型相同:

es=EasySettings('myconfigfile.conf')# Booleanes.set("newuser",True)ifes.get('newuser'):print"now you can use get() as a boolean."# Integeres.set('maxwidth',560)halfwidth=es.get('maxwidth')/2# this math works.# Floates.set('soda',1.59)f_withtax=es.get('soda')*1.08# Listes.set('users',['cjw','joseph','amy'])# lists as settings, very convenientforsuserines.get('users'):print"retrieved user name: "+suser# i won't do them all, but if you can pickle it, you can use it with easysettings.

错误更具描述性,可以使用其专有名称捕获:

try:es.get('option_with_a_possibly_illegal_value')excepteasysettings.esGetErrorasexErr:print"Error getting option!"exceptExceptionasexEx:print"General Error!"

自动创建:

如果将文件名传递给easysettings(),则存在配置文件 函数被调用。如果 文件不存在,否则将返回true。使用"自动 创建"执行此操作:

settings=EasySettings('myconfigfile.conf')# if file exists, all settings were loaded.# if file did not exist, it was created.# No permissions, disk-full, and other errors are still possible of course# depending on the machine, or the current directory permissions.

您可以通过不传递文件来禁用"自动创建"功能 名称,并像t一样单独加载他的:

settings=EasySettings()settings.configfile='myconfigfile.conf'# file has not been created or loaded.# file must exist before calling 'load_file'ifsettings.load_file():# all settings were loaded.else:# unable to load file for some reason.

这与禁用自动创建的方式相同:

settings=EasySettings()# file has not been created or loaded.# file 'myconfigfile.conf' must exist before calling load_file()ifsettings.load_file('myconfigfile.conf'):# file was loaded.# settings.configfile was set by the load_file() functionelse:# file could not be loaded.

要检查文件是否存在而不自动创建,可以执行以下操作 这:

#!/usr/bin/env python# --------------- Creation ----------------fromeasysettingsimportEasySettingssettings=EasySettings("myconfigfile.conf")# configfile_exists() checks for existing config, and creates one if needed.# ** this function is called automatically now when a filename is passed to easysettings. **# if you wish to disable it, just do: settings = EasySettings() and set# settings.configfile later.# ------------- Basic Functions -----------# set without savingsettings.set("username","cjw")settings.set("firstrun",False)printsettings.get("username")# this results in "cjw"# check if file is savedifnotsettings.is_saved():print"you haven't saved the settings to disk yet."# ...settings are still available even if they haven't#    been saved to disk# savesettings.save()# you may also set & save in one line...settings.setsave("homedir","/myuserdir")
0

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

推荐PyPI第三方库


热门话题
JAXB可以将ArrayList作为逗号分隔的值输出吗?   java使用bcel将一个内部类移动到另一个外部类   java无法识别Lucene MoreLikeThis中的错误   安卓如何在Frida中将动态类转换为json或java文件   java如何使用Struts2在blob类型的列中保存我的sql中的图像?   使用mavenreleaseplugin将java maven发布到nexus 3.0.1失败   java这是正确的方法吗?   Windows上的java Runner不工作   找不到java Hibernate+Spring xml映射   java如何访问WMI查询的数据(通过JNA)SAFEARRAY结果   java如何在本地导入库而不使用Maven中的Nexus?   java渐变本地项目依赖项   使用URLFetchService/URL Google appengine for java