将 pyparsing 结果与节点链接列表关联的模式

发布于 2024-10-31 19:15:45 字数 1999 浏览 1 评论 0原文

我已经定义了一个 pyparsing 规则来将此文本解析为语法树...

文本命令:

add Iteration name = "Cisco 10M/half"
    append Observation name = "packet loss 1"
        assign Observation results_text = 0.0
        assign Observation results_bool = True
        append DataPoint
            assign DataPoint metric = txpackets
            assign DataPoint units = packets
        append DataPoint
            assign DataPoint metric = txpackets
            assign DataPoint units = packets
    append Observation name = "packet loss 2"
        append DataPoint
            assign DataPoint metric = txpackets
            assign DataPoint units = packets
        append DataPoint
            assign DataPoint metric = txpackets
            assign DataPoint units = packets

语法树:

['add', 'Iteration', ['name', 'Cisco 10M/half']]
['append', 'Observation', ['name', 'packet loss 1']]
['assign', 'Observation', ['results_text', '0.0']]
['assign', 'Observation', ['results_bool', 'True']]
['append', 'DataPoint']
['assign', 'DataPoint', ['metric', 'txpackets']]
['assign', 'DataPoint', ['units', 'packets']]
...

我正在尝试将上面语法树中的所有嵌套键值对关联到对象的链接列表中...层次结构看起来像这样(每个单词都是一个namedtuple...层次结构中的子元素在父母的孩子名单上):

Log: [ 
    Iteration: [ 
        Observation: 
            [DataPoint, DataPoint], 
        Observation: 
            [DataPoint, DataPoint]
    ]
]

所有这一切的目标是构建一个通用的测试数据采集平台,以驱动针对网络设备的测试流程并记录结果。数据采用这种格式后,将使用相同的数据结构来构建测试报告。为了回答下面评论中的问题,我选择了一个链接列表,因为这似乎是在编写报告时按顺序使信息出队的最简单方法。但是,我宁愿在完成测试之前不分配迭代或观察序列号...以防我们在测试过程中发现问题并插入更多观察。我的理论是列表中每个元素的位置就足够了,但如果它是问题的一部分,我愿意更改它。

问题是我在尝试在构建链接列表后将键值分配给链接列表中的对象时迷失了方向。例如,在我将 Observation namedtuple 插入第一个 Iteration 后,我无法可靠地处理 assign Observation results_bool = 的更新上例中为 True

是否有通用的设计模式来处理这种情况?我已经用谷歌搜索了一段时间,但我似乎无法在解析文本(我可以做到)和管理数据层次结构(主要问题)之间建立联系。超链接或小型演示代码都可以...我只需要指针即可走上正确的轨道。

I have defined a pyparsing rule to parse this text into a syntax-tree...

TEXT COMMANDS:

add Iteration name = "Cisco 10M/half"
    append Observation name = "packet loss 1"
        assign Observation results_text = 0.0
        assign Observation results_bool = True
        append DataPoint
            assign DataPoint metric = txpackets
            assign DataPoint units = packets
        append DataPoint
            assign DataPoint metric = txpackets
            assign DataPoint units = packets
    append Observation name = "packet loss 2"
        append DataPoint
            assign DataPoint metric = txpackets
            assign DataPoint units = packets
        append DataPoint
            assign DataPoint metric = txpackets
            assign DataPoint units = packets

SYNTAX TREE:

['add', 'Iteration', ['name', 'Cisco 10M/half']]
['append', 'Observation', ['name', 'packet loss 1']]
['assign', 'Observation', ['results_text', '0.0']]
['assign', 'Observation', ['results_bool', 'True']]
['append', 'DataPoint']
['assign', 'DataPoint', ['metric', 'txpackets']]
['assign', 'DataPoint', ['units', 'packets']]
...

I'm trying to associate all the nested key-value pairs in the syntax-tree above into a linked-list of objects... the heirarchy looks something like this (each word is a namedtuple... children in the heirarchy are on the parents' list of children):

Log: [ 
    Iteration: [ 
        Observation: 
            [DataPoint, DataPoint], 
        Observation: 
            [DataPoint, DataPoint]
    ]
]

The goal of all this is to build a generic test data-acquisition platform to drive the flow of tests against network gear, and record the results. After the data is in this format, the same data structure will be used to build a test report. To answer the question in the comments below, I chose a linked list because it seemed like the easiest way to sequentially dequeue the information when writing the report. However, I would rather not assign Iteration or Observation sequence numbers before finishing the tests... in case we find problems and insert more Observations in the course of conducting the test. My theory is that the position of each element in the list is sufficient, but I'm willing to change that if it's part of the problem.

The problem is that I'm getting lost trying to assign Key-Values to objects in the linked list after it's built. For instance, after I insert an Observation namedtuple into the first Iteration, I have trouble reliably handling the update of assign Observation results_bool = True in the example above.

Is there a generalized design pattern to handle this situation? I have googled this for while, but I can't seem to make the link between parsing the text (which I can do) and managing the data-heirarchy (the main problem). Hyperlinks or small demo code is fine... I just need pointers to get on the right track.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷情妓 2024-11-07 19:15:45

我不知道您正在寻找的实际设计模式,但我对当前的问题充满热情。我大量使用网络设备,解析和组织数据对我来说是一个巨大的持续挑战。

很明显,问题不在于解析数据,而在于解析后如何处理数据。这是您需要考虑附加到已解析的数据的含义的地方。如果包含列表的对象也有意义,则嵌套列表方法可能适合您。

命名元组非常适合快速而肮脏的类行为,但是当您需要它们执行基本属性访问之外的任何操作时,特别是考虑到它们作为元组是不可变的,它们就会失效。在我看来,您可能希望用成熟的类替换某些 namedtuple 对象。通过这种方式,您可以高度自定义可用的行为和方法。

例如,您知道 Iteration 将始终包含 1 个或多个 Observation 对象,然后这些对象将包含 1 个或多个 DataPoint 对象。如果你能准确地描述这些关系,这将为你指明处理这些关系的道路。

I am not aware of an actual design pattern for what you're looking for, but I have a great passion for the issue at hand. I work heavily with network devices and parsing and organizing the data is a large ongoing challenge for me.

It's clear that the problem is not parsing the data, but what you do with it afterwards. This is where you need to think about the meaning you are attaching to the data you have parsed. The nested-list method might work well for you if the objects containing the lists are also meaningful.

Namedtuples are great for quick-and-dirty class-ish behavior, but they fall flat when you need them to do anything outside of basic attribute access, especially considering that as tuples they are immutable. It seems to me that you'll want to replace certain namedtuple objects with full-blown classes. This way you can highly customize the behavior and methods available.

For example, you know that an Iteration will always contain 1 or more Observation objects which will then contain 1 or more DataPoint objects. If you can accurately describe the relationships, this sets you on the path to handling them.

最偏执的依靠 2024-11-07 19:15:45

我最终使用了 textfsm,它允许我在解析配置文件时保持不同行之间的状态。

I wound up using textfsm, which allows me to keep state between different lines while parsing the configuration file.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文