机器人词库 python字典,如何在python和Robotframework中对字典进行切片?

只提供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}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值