没有项目描述

python-jsonstore的Python项目详细描述


PyPi PackageBuild StatusCodacy Rating

jsonstore

此模块提供一个类,该类映射来自json文件的键和值。 在它的属性上。

目的是提供一种方便的装载和保存方法 以熟悉的可读格式配置。这是多一点 比灵活 configparser python中包含的模块。

这项工作是在python 2.7+和python 3.3+上测试和工作的。会的 不适用于2.6或更低版本,但预计适用于3.0-3.2版本。测试 由于以下原因,不在3.2.6中工作 mistreating 解析测试代码时的。这也在pypy和pypy3上进行了测试。

示例

基础知识

# by default JsonStore commits on every change unless in a transactionstore=JsonStore('config.json')store.a_string="something"store.a_list=[1,2,3]store.a_dictionary={'dict-list':[{}],'ln(2)':0.69314718056,'for-you':u"?",}# you can use […] to set/get/delete string keysstore['some_key']="a value"# the key is split on '.'s and works on dictionariesdelstore['a_dictionary.dict-list']store['a_dictionary.new_value']="old value"#  you can also use the syntactic sugar for tuple keys (explicit lists work too)assertstore['a_dictionary','new_value']=="old value"# you can traverse lists tooassertstore['a_list',-1]==3# you can use slices in listsassertlen(store['a_list',1:])==2# deep copies are made when assigning valuesmy_list=['fun']store.a_list=my_listassertstore.a_listisnotmy_listassert'a_list'instore# deep copies are also returned to avoid unsanitary changes being madestore.a_dictionary['new_value']="new value"# won't update the store!assertstore.a_dictionary['new_value']=="old value"assertstore.a_dictionaryisnotstore.a_dictionary

交易

JsonStore对象可以用作context managers来提供 在发生异常时回滚的事务。这个 事务模型是原始的;您只能嵌套事务。

当将存储放入事务中时,它不会将更改保存到 在所有交易结束前归档。

fromjsonstoreimportJsonStore# even with auto_commit=True, the file won't be saved until the last contexts has been closedwithJsonStore('config.json',indent=None,auto_commit=False)asstore:self.value=1# the context manager will roll back changes made if an exception is raisedstore=JsonStore('config.json',indent=None)try:withstore:store.value="new"raiseExceptionexceptException:pass# here we see the value that was saved previouslyassertstore.value==1

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

推荐PyPI第三方库


热门话题
java将Int值传递给另一个类   在使用Sdin(或非交互式)输入| Java时调试奇怪的输入错误   java返回一个文本字符串作为响应struts2   java为什么我们不能在声明局部变量之前初始化它呢?   java从现有XML中读取值并将其填充到Json中   java Spring Boot+Maven:找不到repositoryFactoryBean   java使用regex表示“W o.Rd”。replaceAll(“单词”、“替换”)   java Sub resorce在Jersey REST API框架中不起作用   java在组件启动时,当所述报告存储在Sharepoint中时,是否可以加载预格式化的WebDataRocks报告?   java并发更新列表的最佳方法   servlets Java web应用程序对象调度   应用程序在点击按钮时崩溃,Android Studio(Java)   java如何为掷骰子游戏调用另一个类中的方法?