在 Robot Framework 中实现有序字典 [英] Implement an ordered dictionary in Robot Framework
问题描述
我想在 Robot Framework 中有一个有序的字典.有没有我可以用于此目的的库?我该怎么做?
我不知道有任何库有创建有序 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} 评估 sys.modules['robot.utils'].OrderedDict() sys # RF Ordered Dict
在 Python 中,使用它看起来像这样:
# 只使用其中一种from robots.utils import OrderedDict # RF Ordered Dict从集合导入 OrderedDict # Python OrderedDictod = OrderedDict()
请注意,集合中的关键字在设计时并未考虑到有序的字典.例如 Get Dictionary Keys
对键进行排序.要解决此问题,您可以直接调用 keys 方法:
${keys} 调用方法 ${od} 键
或
${keys} 设置变量 ${od.keys()}
类似地,Get Dictionary Items
、Get Dictionary Values
和 Log Dictionary
涉及排序,可能会破坏 OrderedDict 的顺序.>
I want to have an ordered dictionary in Robot Framework. Is there any libraries which I can use for this purpose? How can I do that?
I am not aware of any library that has a keyword to create an ordered dict, but creating an ordered dict is simple enough.
As of Python 2.7, there is an ordered dictionary available from Python. Robot Framework also defines one that is available in Python/Jython versions prior to 2.7. The Robot Framework OrderedDict has more features than the Python one.
Creating an ordered dict in Robot Framework would look like this:
${od} Evaluate collections.OrderedDict() collections # Python OrderedDict
${od} Evaluate sys.modules['robot.utils'].OrderedDict() sys # RF Ordered Dict
In Python, using it would look like this:
# use only one of these
from robot.utils import OrderedDict # RF Ordered Dict
from collections import OrderedDict # Python OrderedDict
od = OrderedDict()
Beware that the keywords from Collections were not designed with ordered dicts in mind. For instance Get Dictionary Keys
sorts the keys.
To workaround that, you can invoke the keys method directly:
${keys} Call Method ${od} keys
or
${keys} Set Variable ${od.keys()}
Similarly, Get Dictionary Items
, Get Dictionary Values
, and Log Dictionary
involve sorting, possibly destroying the order of an OrderedDict.
这篇关于在 Robot Framework 中实现有序字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!