简体   繁体   English

查询嵌套的 python 对象

[英]query on nested python objects

I have some objects structure created with YAML.我有一些用 YAML 创建的对象结构。 Inside YAML many tags are used.在 YAML 中使用了许多标签。 So the document contains not only builtin types but also some objects created from registered constructors.因此该文档不仅包含内置类型,还包含一些从注册构造函数创建的对象。 I need to check them by some patterns.我需要通过一些模式来检查它们。

So far I've tried jsonpath , objectpath , jmespath and dpath .到目前为止,我已经尝试过jsonpathobjectpathjmespathdpath All those libraries are great and I like query languages (especially in first two), but they are strictly expecting JSON as the input.所有这些库都很棒,我喜欢查询语言(尤其是前两个),但他们严格要求JSON作为输入。 Is there some analogs for raw objects?是否有一些原始对象的类似物?

From what I remember writing ObjectPath there are technical nuances which makes querying Python objects hard to impossible.从我记得写 ObjectPath 的内容来看,有一些技术上的细微差别,这使得查询 Python 对象很难甚至不可能。 There are built in Objects and classes written in C that do not expose object properties, there are private properties hidden for other classes etc.有内置的对象和用 C 编写的类,它们不公开对象属性,还有为其他类隐藏的私有属性等。

ObjectPath should work well with classes that inherit from Dict, but the code is experimental. ObjectPath 应该适用于从 Dict 继承的类,但代码是实验性的。 It may work out of the box for you and if not, the code that does that is in newest ObjectPath @ https://github.com/adriank/ObjectPath/blob/master/objectpath/core/interpreter.py#L47 .它可能对您来说是开箱即用的,如果不是,那么在最新的 ObjectPath @ https://github.com/adriank/ObjectPath/blob/master/objectpath/core/interpreter.py#L47中执行此操作的代码。

glom handle this: glom处理这个:

>>> from glom import glom
>>> from types import SimpleNamespace
>>> sn1 = SimpleNamespace()
>>> sn1.foo = SimpleNamespace()
>>> sn1.foo.bar = "baz"
>>> glom(sn1, "foo.bar")
'baz'

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM