只提供2个选择使用机器人框架关键字。本质上,他们遵循类似的方法。从字典中获取键,然后切片,然后以所需的格式修改或重新创建字典。在
除非有特定的原因不想为此使用Python,否则我认为这个功能应该由Python关键字而不是Robot框架来提供。在*** Settings ***
Library Collections
*** Variables ***
&{dict1} 1=a 2=b 3=c 4=d 5=e
&{dict2} 1=a 2=b 3=c 4=d 5=e
&{result} 3=c 4=d 5=e
*** Test Cases ***
TC - keep items 3, 4 & 5
# Keey
Keep Slice In Dictionary ${dict1} ${5} ${2}
Log Many ${dict1}
Dictionaries Should Be Equal ${dict1} ${result}
${slice} Get Slice From Dictionary ${dict2} ${5} ${2}
Log Many ${slice}
Dictionaries Should Be Equal ${dict1} ${slice}
*** Keywords ***
Keep Slice In Dictionary
[Documentation]
... Modifies the dictionary to only leave the slice.
...
... The keys of the dictionary are converted into a list. Then
... this list is spliced. This list is then used to filter out
... the unwanted keys.
...
... Note: this keyword modifies the provided dictionary.
...
... Arguments:
... - dict (dictionary) The dictionary that needs to be modified
... - high (integer) The last item to be kept.
... - low (integer) The first item of the slice. (defaults to 0)
...
... Returns: None Modifies the provided dictionary.
...
[Arguments] ${dict} ${high} ${low}=${0}
${keys_list} Get Dictionary Keys ${dict}
${filtered_keys} Get Slice From List ${keys_list} ${low} ${high}
Keep In Dictionary ${dict} @{filtered_keys}
Get Slice From Dictionary
[Documentation]
... Get a slice of sequential keys from a dictionary
...
... The keys of the dictionary are converted into a list. Then
... this list is spliced. This list is then used to create a new
... Dictionary with the filtered keys.
...
... Arguments:
... - dict (dictionary) The source dictionary
... - high (integer) The last item to be kept.
... - low (integer) The first item of the slice. (defaults to 0)
...
... Returns: (dictionary A dictionary with the desired keys.
...
[Arguments] ${dict} ${high} ${low}=${0}
${keys_list} Get Dictionary Keys ${dict}
${filtered_keys} Get Slice From List ${keys_list} ${low} ${high}
${return_dict} Create Dictionary
:FOR ${item} IN @{filtered_keys}
\ Set To Dictionary ${return_dict} ${item} ${dict['${item}']}
[Return] ${return_dict}