【问题标题】:Modifying lists of lists in Robot Framework修改 Robot Framework 中的列表列表
【发布时间】:2011-09-07 07:12:54
【问题描述】:

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

我的列表如下所示:

[鲍勃,玛丽,[六月,七月,八月]]

我想把“七月”改成别的,说“九月”

Robot Framework 可以让我更改 'bob' 或 'mary',但如果我尝试插入一个列表,它会被转换为字符串。

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

【问题讨论】:

    标签: robotframework


    【解决方案1】:

    我可以使用这样的 Collections 库关键字来实现修改

    *** 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} 
    

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

    【讨论】:

    • 谢谢。这看起来可行,但有点费力。
    【解决方案2】:

    我从缺乏响应中猜测,没有一个干净整洁的解决方案。这是我所做的:

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

    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”

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

    【讨论】:

    • 不管怎样,我认为创建自定义关键字确实是解决这个问题的最优雅的解决方案。
    【解决方案3】:

    Collections 库中的常规方法“Set List Value”确实适用于嵌入列表 - 并且它已就地更改,无需重新创建对象;这是 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
    

    【讨论】:

      猜你喜欢
      • 2017-02-28
      • 1970-01-01
      • 2017-06-18
      • 2017-12-02
      • 2021-01-16
      • 1970-01-01
      • 1970-01-01
      • 2019-07-22
      • 1970-01-01
      相关资源
      最近更新 更多