基于jq语法的简单过滤
jqfilters的Python项目详细描述
过滤器
jqfilters
允许轻松创建过滤器对象,这些对象可以应用于类似JSON的字典。
它基于流行的JSON解析工具jq,因此
语法模仿jq
为了显示两者,将使用相同的JSON对象:
>>>person={..."name":"Alice"..."books":[...{"name":"The name of the wind"},...{"name":"A feast for crows"},...{"name":"Continuous delivery"}...],..."age":24,..."location":"Lancashire"...}
简单查询
我们将创建一个过滤器,它将为居住在约克郡的人返回True
。在
我们还可以对操作数应用一些转换(请参见jqfilters.operations
):
>>>fromjqfiltersimportFilter>>>reads_a_lot=Filter(op1='.books',transform1='len',operator='ge',op2=3)>>>reads_a_lot(person)True
复杂查询
我们将创建一个过滤器,当任何一本书的名字是“权力的游戏”时,它将返回True
或者是18岁以上的人。由于此规范更为复杂,因此
:meth:将使用fromConfig <jqfilters.filters.Filter.fromConfig>
方法。在
>>>specs={..."op1":{..."op1":".books[].name",..."operator":"contains",..."op2":"A game of thrones"...},..."operator":"or",..."op2":{..."op1":".age",..."operator":"ge",..."op2":18...}...}>>>adult_got_fans=Filter.fromConfig(specs)>>>adult_got_fans(person)True
只需提示过滤器,就可以以更简单的方式检查它:
>>>adult_got_fans>>>((.books[].name-contains-Agameofthrones)-or-(.age-ge-18))
- 项目
标签: