修改Robot Framework中的列表列表

发布于 2024-11-13 03:25:53 字数 288 浏览 5 评论 0原文

我有一个在机器人框架中使用的嵌套列表。我想更改机器人框架级别的子列表中的一项。

我的列表如下所示:

[ bob, mary, [june, july, august]]

我想将“july”更改为其他内容,例如“september”,

Robot Framework 将允许我更改“bob”或“mary”,但是如果我尝试插入一个列表,它被转换为字符串。

(哦,我尝试过使用“Insert Into List 关键字来插入新的子列表,以及其他 List 关键字,但没有任何运气。)

I have a nested list that I am using in Robot Framework. I would like to change one item in a sublist at the Robot Framework level.

My list looks like this:

[ bob, mary, [june, july, august]]

I want to change "july" to something else, say "september"

Robot Framework will let me change 'bob' or 'mary', but if I try to insert a list, it is converted into strings.

(Oh, I have tried to use the "Insert Into List keyword to insert a new sub list, and other List keywords, no luck with any.)

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

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

发布评论

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

评论(3

抚你发端 2024-11-20 03:25:53

我能够使用集合库关键字实现修改,就像这样

*** settings ***                                                                       
Library   Collections                                                                

*** test cases ***                                                                     
test    ${l1}=  Create List  1  2  3                                        
        ${l2}=  Create List  foo  bar  ${l1}                                              
        ${sub}=  Get From List  ${l2}  2 
        Set List Value   ${sub}   2   400 
        Set List Value   ${l2}  2  ${sub}  
        Log  ${l2} 

我无法找到直接更改子列表的方法,必须首先提取它,然后修改,最后放回原位。

I was able to achieve the modification with Collections library keywords like this

*** settings ***                                                                       
Library   Collections                                                                

*** test cases ***                                                                     
test    ${l1}=  Create List  1  2  3                                        
        ${l2}=  Create List  foo  bar  ${l1}                                              
        ${sub}=  Get From List  ${l2}  2 
        Set List Value   ${sub}   2   400 
        Set List Value   ${l2}  2  ${sub}  
        Log  ${l2} 

I was not able to find a way to directly alter the sublist, it has to be first extracted, then modified and finally put back in place.

话少情深 2024-11-20 03:25:53

由于缺乏回应,我猜测对此没有一个简洁干净的解决方案。这是我所做的:

我因此创建了一个实用程序:

class Pybot_Utilities:
    def sublistReplace(self, processList, item, SublistIndex, ItemIndex):
        '''
        Replaces an item in a sublist
        Takes a list, an object, an index to the sublist, and an index to a location in the sublist inserts the object into a sublist of the list at the location specified. 
        So if the list STUFF is (X, Y, (A,B,C)) and you want to change B to FOO give these parameters: [STUFF, FOO, 2, 1]
        '''

        SublistIndex=int(SublistIndex)
        ItemIndex=int(ItemIndex)
        processList[SublistIndex][ItemIndex] = str(item)
        return processList

然后将此条目放入我的机器人框架测试套件文件中:(

|    | ${ListWithSublist} = | sublistReplace    | ${ListWithSublist]}  | NewItem | 1 | 1 |

当然,导入我的实用程序库)

运行后,子列表中的第二项(索引 1)位于列表的索引 1 将是“NewItem”

也许不是最优雅或最灵活的,但它现在可以完成工作

I am guessing from the lack of response that there isn't a neat clean solution to this. Here is what I have done:

I created a utility thusly:

class Pybot_Utilities:
    def sublistReplace(self, processList, item, SublistIndex, ItemIndex):
        '''
        Replaces an item in a sublist
        Takes a list, an object, an index to the sublist, and an index to a location in the sublist inserts the object into a sublist of the list at the location specified. 
        So if the list STUFF is (X, Y, (A,B,C)) and you want to change B to FOO give these parameters: [STUFF, FOO, 2, 1]
        '''

        SublistIndex=int(SublistIndex)
        ItemIndex=int(ItemIndex)
        processList[SublistIndex][ItemIndex] = str(item)
        return processList

I then put this entry in my robot framework test suite file:

|    | ${ListWithSublist} = | sublistReplace    | ${ListWithSublist]}  | NewItem | 1 | 1 |

(Importing my utility library, of course)

After this runs, the second item (index 1) in the sublist at index 1 of the list will be "NewItem"

Perhaps not the most elegant or flexible, but it will do the job for now

初懵 2024-11-20 03:25:53

集合库中的普通方法“设置列表值”确实适用于嵌入列表 - 并且它就地更改,无需重新创建对象;这是概念验证:

${listy}=   Create List     a   b
${inner}=   Create List     1   2
Append To List      ${listy}       ${inner}
Log To Console      ${listy}      # prints "[u'a', u'b', [u'1', u'2']]", as expected

Set List Value      ${listy[2]}    0       4
# ^ changes the 1st element of the embedded list to "4" - both the listy's index (2), and the kw argument (0) can be variables

Log To Console      ${listy}      # prints "[u'a', u'b', [u'4', u'2']]" - i.e. updated

The normal method "Set List Value" in the Collections library does work on the embedded list - and its changed inplace, w/o recreating the objects; here's the POC:

${listy}=   Create List     a   b
${inner}=   Create List     1   2
Append To List      ${listy}       ${inner}
Log To Console      ${listy}      # prints "[u'a', u'b', [u'1', u'2']]", as expected

Set List Value      ${listy[2]}    0       4
# ^ changes the 1st element of the embedded list to "4" - both the listy's index (2), and the kw argument (0) can be variables

Log To Console      ${listy}      # prints "[u'a', u'b', [u'4', u'2']]" - i.e. updated
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文