具有三重嵌套字典的机器人框架变量类文件不可访问点符号 [英] Robot Framework Variable Class File with triple Nested Dictionary is not dot notation accessible

查看:39
本文介绍了具有三重嵌套字典的机器人框架变量类文件不可访问点符号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用关于变量文件的机器人框架文档作为指南我使用 get_variables<实现了一个变量文件类/a>.基本示例按描述工作.

Using the Robot Framework Documentation on Variable Files as a guide I implemented a Variable File Class with the get_variables. The basic example works as described.

当我实现三重嵌套字典 (${ABC}) 时,我可以使用 ${A}${AB}<访问前两个/code> 符号.但是,当我想访问第三个节点 ${A.B.C} 时,结果是我收到错误

When I implement a triple nested Dictionary (${A.B.C}) I can access the first two using ${A} and ${A.B} notation. However, when I want to access the third node ${A.B.C} the result is that I get an error

Resolving variable '${A.B.C}' failed: AttributeError: 'OrderedDict' object
has no attribute 'C'

在下面的三个示例中,我有一个 RF 生成的嵌套字典,我可以通过点符号访问所有节点.第二个例子是一个由变量类返回的普通 Python 字典.在最后一个示例中,返回的 Variable 类的类型为 OrderedDict.

In the below three examples I have an RF generated nested dictionary that I can access all nodes through the dot notation. The second example is a plain Python dictionary that is given back by the Variable Class. In the last example the Variable class returned is of the type OrderedDict.

虽然 ${A['B']['C']['key']} 有效,但在代码中更难阅读.当我加载一个类似的 yaml 结构时,它完全支持点表示法,但这不是一个选项,因为 yaml 文件是静态的,而且我需要 Python 的一些关键值的灵活性.

Although the ${A['B']['C']['key']} works, it is more difficult to read in the code. When I load a similar yaml structure it supports the dot notation fully but this is not an option as the yaml file is static, and I require flexibility of Python for some of the key values.

因此,我正在寻找有关如何返回允许 Robot Framework 使用点符号解释完整嵌套结构的数据结构的支持.

So, I'm looking for some support on how to return a data structure that allows Robot Framework to interpret the full nested structure with the dot notation.

变量类文件

from collections import OrderedDict

class OrderDict(object):
    def get_variables(self):
        C = OrderedDict([(u'key', u'value')])
        B = OrderedDict([(u'C', C)])
        A = OrderedDict([(u'B', B)])
        D = {
                u'E':
                    {
                    u'F':
                        {
                            u'key':  u'value'
                        }
                    }
            }
        return OrderedDict([(u'DICT__A', A), (u'DICT__D', D)])

机器人框架脚本

*** Test Cases ***
Dictionary RF
    ${Z}    Create Dictionary    key=value
    ${Y}    Create Dictionary    Z=${Z}
    ${X}    Create Dictionary    Y=${Y}
    Log To Console    ${EMPTY}
    Log To Console    ${X}
    Log To Console    ${X['Y']['Z']['key']}
    Log To Console    ${X.Y}
    Log To Console    ${X.Y.Z}
    Log To Console    ${X.Y.Z.key}

Plain Dictionary Variable Class
    Log To Console    ${EMPTY}
    Log To Console    ${D}
    Log To Console    ${D['E']['F']['key']}
    Log To Console    ${D.E}
    Log To Console    ${D.E.F}
    Log To Console    ${D.E.F.key}

Ordered Dictionary Variable Class
    Log To Console    ${EMPTY}
    Log To Console    ${A}
    Log To Console    ${A['B']['C']['key']}
    Log To Console    ${A.B}
    Log To Console    ${A.B.C}
    Log To Console    ${A.B.C.key}

机器人框架控制台日志

套件执行器:Robot Framework 3.0.2(Win32 上的 Python 2.7.9)

Suite Executor: Robot Framework 3.0.2 (Python 2.7.9 on win32)

Dictionary RF                                                         
{u'Y': {u'Z': {u'key': u'value'}}}
value
{u'Z': {u'key': u'value'}}
{u'key': u'value'}
value
| PASS |
------------------------------------------------------------------------------
Plain Dictionary Variable Class                                       
{u'E': {u'F': {u'key': u'value'}}}
value
{u'F': {u'key': u'value'}}
| FAIL |
Resolving variable '${D.E.F}' failed: AttributeError: 'dict' object has no attribute 'F'
------------------------------------------------------------------------------
Ordered Dictionary Variable Class                                     
{u'B': OrderedDict([(u'C', OrderedDict([(u'key', u'value')]))])}
value
OrderedDict([(u'C', OrderedDict([(u'key', u'value')]))])
| FAIL |
Resolving variable '${A.B.C}' failed: AttributeError: 'OrderedDict' object has no 
attribute 'C'

推荐答案

Robot Framework Slack频道 Pekka Klarck指出Robot Framework内部使用了robot.utils.DotDic类.让 get_variables() 返回一个 DotDic 结构解决了我的问题,我现在可以使用点表示法.下面是变量类 DotDic 的代码(存储为 DotDic.py).

In the Robot Framework Slack channel Pekka Klarck pointed out that Robot Framework internally uses the robot.utils.DotDic class. Having get_variables() return a DotDic structure resolved my issue and I can now use the dot notation. Below is the code for the Variable Class DotDic (stored as DotDic.py).

from robot.utils import DotDict

class DotDic(object):
    def get_variables(self):
        G = {
                u'H':
                    {
                    u'I':
                        {
                            u'key':  u'value'
                        }
                    }
            }
        return {u'G': self.dict_to_dotdict(G)}

    def dict_to_dotdict(self, dct):
        dd = DotDict({})
        for key, val in dct.items():
            if isinstance(val, dict):
                dd[key] = self.dict_to_dotdict(val)
            else:
                dd[key] = val
        return dd

这篇关于具有三重嵌套字典的机器人框架变量类文件不可访问点符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆