在 Robot Framework 中实现有序字典
Implement an ordered dictionary in Robot Framework
我想在 Robot Framework 中有一个有序的字典。有没有我可以用于此目的的库?我该怎么做?
我不知道有任何库具有创建有序字典的关键字,但创建有序字典非常简单。
从 Python 2.7 开始,Python 提供了一个有序字典。
Robot Framework 还定义了一个在 2.7 之前的 Python/Jython 版本中可用的。机器人框架 OrderedDict 比 Python 具有更多的功能。
在 Robot Framework 中创建有序字典如下所示:
${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 中的关键字在设计时并未考虑到有序的字典。例如 Get Dictionary Keys
对键进行排序。
要解决此问题,您可以直接调用 keys 方法:
${keys} Call Method ${od} keys
或
${keys} Set Variable ${od.keys()}
类似地,Get Dictionary Items
、Get Dictionary Values
和Log Dictionary
涉及排序,可能会破坏 OrderedDict 的顺序。
我想在 Robot Framework 中有一个有序的字典。有没有我可以用于此目的的库?我该怎么做?
我不知道有任何库具有创建有序字典的关键字,但创建有序字典非常简单。
从 Python 2.7 开始,Python 提供了一个有序字典。 Robot Framework 还定义了一个在 2.7 之前的 Python/Jython 版本中可用的。机器人框架 OrderedDict 比 Python 具有更多的功能。
在 Robot Framework 中创建有序字典如下所示:
${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 中的关键字在设计时并未考虑到有序的字典。例如 Get Dictionary Keys
对键进行排序。
要解决此问题,您可以直接调用 keys 方法:
${keys} Call Method ${od} keys
或
${keys} Set Variable ${od.keys()}
类似地,Get Dictionary Items
、Get Dictionary Values
和Log Dictionary
涉及排序,可能会破坏 OrderedDict 的顺序。