用于数据操作和管理的python实用程序。
gems的Python项目详细描述
用于数据操作和管理的python实用程序。
安装
通过Github:
~$ git clone http://github.com/bprinty/gems.git
~$ cd gems
~$ python setup.py install
通过PIP:
~$ pip install gems
文档
可以找到该包的文档here。
用法
gems模块提供专门的数据结构来增强开发。它类似于collections模块,但包含不同类型的对象。
目前,以下对象可用(此列表将随时间和反馈而增长):
Name | Description |
---|---|
composite | JSON-like data structure for easy data traversal. |
filetree | JSON-like data structure for easy filesystem traversal. |
复合
<^ > ^ {tt1} $对象抽象了与管理嵌套的基于JSON的结构相关的复杂性,允许更容易地访问内部属性,并提供以直观方式与数据一起工作的运算符。下面是一个如何在项目中使用composite类型的简单示例:>>>fromgemsimportcomposite>>>>>>data=composite({>>>'one':1,>>>'two':[1,2,3],>>>'three':['one',2,{'three':'four'}],>>>'four':{'five':[6,7,8],'nine':10,'eleven':'twelve'}>>>})>>>data.four.five[1]==6True>>>data.two[0]==1True
在上面的示例中,任意数据结构作为composite对象的参数提供,并转换为可以更优雅地(语法上)遍历属性的对象。您还可以从json或yaml文件加载复合对象,如下所示:
>>>fromgemsimportcomposite>>>>>>withopen('data.json','r')asfi:>>>data=composite.load(fi)>>>>>>printdata.four.five[1]6>>>>>>withopen('data.yml','r')asfi:>>>data=composite.load(fi)>>>>>>printdata.four.five[1]6
还有绑定到composite对象的操作。如果添加了两个复合对象或一个复合对象和另一个类似类型,则会得到一个composite对象,该对象以直观的方式组合这些对象:
>>># using the 'data' object from above>>>obj=data+{'five':6}>>>obj.five==6True>>>obj.two===[1,2,3]True>>>obj=data+[1,2,3]>>>obj[0].one.two[0]==1True>>>obj[1][1]==2True>>>data2=composite([1,2,3,{'four':5}])>>>obj=data2+{'five':6}>>>obj[0][0]==1True>>>obj[0][2].four==5True>>>obj=data2+['seven',8,9]>>>obj[4:6]==['seven',8]True
类似这样的其他操作也可以用于composite对象。例如:
>>># using the 'data' object from above>>>'three'indataTrue>>>7indata.four.fiveTrue>>>data.four.five==[6,7,8]True>>>data==data2False
文件树
在python中,遍历文件树通常是一种痛苦。可以在递归函数中使用os.path.walkto来完成它,但是应该有一种更简单的方法。这就是gems.filetree的用武之地。下面是如何在项目中使用gems.filetree类型的示例:
>>>fromgemsimportfiletree>>>>>># mydir is a directory with the structure below>>>ftree=filetree('mydir')>>>printftreemydir/one/two.txtthree.jsontwo/three/four.txtfivesix/seven.txteight.config
gems.filetree结构还允许遍历如下文件数据:
>>>printdata.one['two.txt']/full/path/to/mydir/one/two.txt>>>>>>printdata.two.three['four.txt']/full/path/to/mydir/two/three/four.txt>>>>>>printdata.two['five six']['eight.config']/full/path/to/mydir/two/fivesix/eight.config
使用基于json的访问比执行许多os.path.join操作来创建文件系统上对象的完整路径要简单得多。
问题/反馈
在GitHub issue tracker中提交问题。