pythonic对象表示法-支持本机对象和路径
pyon-lib的Python项目详细描述
pyon
简介
pyon(pythonic json)是一个python库,它允许您轻松地将本机对象转换为json对象。
它还支持类似文件系统的路径结构, 它允许您按照自己喜欢的方式轻松地构造json对象。
此外,它还使用递归来将每个连接的对象转换为可用的形式。
先决条件
- python=3.2
安装
通过pip安装与键入一样简单
pip install pyon-lib
如果要手动安装最新版本,也可以执行以下操作:
git clone https://github.com/lagmoellertim/pyon.git
cd pyon
python3 setup.py install
构建
git clone https://github.com/lagmoellertim/pyon.git
cd pyon
python3 setup.py sdist bdist_wheel
用法
导入pyon
frompyonimportPyonObject
创建基本对象
classTest(PyonObject):def__init__(self):self.var1="Variable 1"self.var2=5.5
将对象转换为json
test=Test()json=test.dump()
这是输出:
{'var1':'Variable 1','var2':5.5}
将json对象写入文件
test=Test()json=test.generate_json(file_object=open("test.json","w+"))
隐藏某些变量
假设您的测试类只需要变量var2进行自己的计算,但您不希望它 最后进入最后一个json对象。通过添加前缀“\u”
classTest(PyonObject):def__init__(self):self.var1="Variable 1"self._var2=5.5test=Test()json=test.dump()
这是输出:
{'var1':'Variable 1'}
使用多个对象/类
作为一个例子,我使用带产品的商店的概念
classStore(PyonObject):def__init__(self,store_name):self.store_name=store_nameself.products=[Product(0,"Smartphone"),Product(1,"Laptop")]classProduct(PyonObject):def__init__(self,article_id,name):self.article_id=article_idself.name=namestore=Store("Generic Store")json=store.dump()
这是输出:
{'store_name':'Generic Store','products':[{'article_id':0,'name':'Smartphone'},{'article_id':1,'name':'Laptop'}]}
指定对象路径
这次,json结构不应该基于类结构,而应该基于路径字符串 我们提供的。
路径字符串与文件系统路径非常相似。您可以使用 这些路径字符串。
下面是一些示例:
path1="/store/"path2="/test/../store/./"# Identical to path1 since ../ means one layer up and ./ can be ignoredpath3="/store/products/*"#The star symbol tells pyon to create a list instead of a dict in this location
让我们修改store/products类,使其使用自定义路径
classStore(PyonObject):def__init__(self,store_name):super().__init__("/stores/*")self.store_name=store_nameself.products=[Product(0,"Smartphone"),Product(1,"Laptop")]classProduct(PyonObject):def__init__(self,article_id,name):super().__init__("products/*")self.article_id=article_idself.name=namestore=Store("Generic Store")json=store.dump()
这是输出:
{'stores':[{'store_name':'Generic Store','products':[{'article_id':0,'name':'Smartphone'},{'article_id':1,'name':'Laptop'}]}]}
正如您在上面的示例中所看到的,只要对象有父对象,相对路径也是可能的。
对路径字符串使用对象变量
要在路径字符串中使用对象变量,只需在字符串中使用括号指定它们,如 这个:{var1}
{var1}的值是self.var1,所以您只需离开“self.”。
classTest(PyonObject):def__init__(self):super().__init__("/test/{var1}-{_var2}/")self.var1="Variable 1"self._var2=5.5test=Test()json=test.dump()
这是输出:
{'test':{'Variable 1-5.5':{'var1':'Variable 1'}}}
不同的转储方法
pyon支持转储pyonobject的不同方法。您可以使用普通的python字典(“json”)、“xml”或 “亚姆”。 下面是一些用法示例:
json=test.dump(output_format="json")# JSON is default, so you can also leave it emptyxml=test.dump(output_format="xml")yaml=test.dump(output_format="yaml")
允许覆盖
最后,还有覆盖保护。因为路径允许您自由选择 对象应该结束,它们有可能重叠。要允许/停止覆盖,可以执行以下操作:
json=test.dump(allow_overwrite=True)
允许覆盖的默认值为false
文档
如果在尝试使用api时遇到问题,请查看代码。它是完全评论和良好的标签, 这会帮助你了解发生了什么。
贡献
如果你错过了一个功能或有了新的想法,就去做吧!这就是开源的目的!
作者
tim luca lagmeller(@lagmoellertim)
捐赠
您也可以通过buying me a coffee进行贡献。
许可证
版权所有©2019-至今,Tim-Luca Lagmöller