【问题标题】:Implement an ordered dictionary in Robot Framework在 Robot Framework 中实现有序字典
【发布时间】:2016-05-09 20:12:46
【问题描述】:

我想在 Robot Framework 中有一个有序的字典。有没有我可以用于此目的的库?我该怎么做?

【问题讨论】:

    标签: robotframework ordereddictionary


    【解决方案1】:

    我不知道有任何库有关键字来创建有序 dict,但创建有序 dict 很简单。

    从 Python 2.7 开始,Python 提供了一个有序字典。 Robot Framework 还定义了一个在 2.7 之前的 Python/Jython 版本中可用的框架。 Robot Framework OrderedDict 比 Python 有更多的功能。

    在 Robot Framework 中创建有序 dict 如下所示:

    ${od}    Evaluate    collections.OrderedDict()    collections    # Python OrderedDict
    ${od}    Evaluate    sys.modules['robot.utils'].OrderedDict()    sys    # RF Ordered Dict
    

    在 Python 中,使用它看起来像这样:

    # use only one of these
    from robot.utils import OrderedDict # RF Ordered Dict
    from collections import OrderedDict # Python OrderedDict
    od = OrderedDict()
    

    请注意,Collections 中的关键字在设计时并未考虑到有序 dicts。例如Get Dictionary Keys 对键进行排序。 要解决这个问题,您可以直接调用 keys 方法:

    ${keys}    Call Method    ${od}    keys
    

    ${keys}    Set Variable    ${od.keys()}
    

    同样,Get Dictionary ItemsGet Dictionary ValuesLog Dictionary 涉及排序,可能会破坏 OrderedDict 的顺序。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-11-28
      • 2015-10-10
      • 2017-02-28
      • 2014-09-24
      • 2018-03-18
      • 1970-01-01
      • 2021-01-11
      • 1970-01-01
      相关资源
      最近更新 更多