Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
836 views
in Technique[技术] by (71.8m points)

robotframework - Luhn algorithm Robot Framework

I am new to robot framework and trying to implement Luhn Algorithm. I found the code in python and want to change it to Robot framework.

def cardLuhnChecksumIsValid(card_number):
""" checks to make sure that the card passes a luhn mod-10 checksum """

sum = 0
num_digits = len(card_number)
oddeven = num_digits & 1

for count in range(0, num_digits):
    digit = int(card_number[count])

    if not (( count & 1 ) ^ oddeven ):
        digit = digit * 2
    if digit > 9:
        digit = digit - 9

    sum = sum + digit

return ( (sum % 10) == 0 )

I started with robotscript and was stuck at statement if not (( count & 1 ) ^ oddeven ) can someone please help me convert the above code to robot script is there a automated to change python code to robot ?

cardLuhnChecksumIsValid
[Arguments]    ${ICCID1}
${num_digits}    Get length    ${ICCID1}
${odd_even}    ${num_digits}
...    AND    ${1}
FOR    ${Count}    IN RANGE    0    ${num_digits}
${digit}    Convert To Integer    ${ICCID[${Count}]}
question from:https://stackoverflow.com/questions/65881409/luhn-algorithm-robot-framework

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Have a look at on the Evaluate keyword in the BuiltIn library. You can use that to rewrite:

if not (( count & 1 ) ^ oddeven ):
    digit = digit * 2

to:

${condition}=    Evaluate    not (( ${count} & 1 ) ^ ${oddeven} )
${digit}=    Run Keyword If    ${condition}    Evaluate    ${digit} * 2

You can rewrite the rest of the algorithm using the same approach. From Robot Framework 3.2 release you can also use Inline Python evaluation.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...