How do I delete an (user) input string stored in a variable from a set IN PYTHON?
Its from this task:
There is an unknown set of objects named mystery_set which has been predefined. You don't have access to this set.
Write a program that deletes the input string (stored in the variable string) from the mystery_set. It is NOT guaranteed that the string is an element of the mystery_set. Don't forget to take that into account!
Don't print anything!
I tried this code but runtime error:
# mystery_set has been defined
string = input()
# delete string from mystery_set
mystery_set.remove(string)
Should I check if string is an element in mystery_set as a way
Thank you
Related
Create user //Is my test case name
${random_string}= Generate Random String 8 [LOWER]
Set Global Variable ${random_string}
${body}= create dictionary email=${random_string}#mail.com firstName=${random_string} lastName=${random_string} role=ADMIN
${response}= Post On Session mysession /user json=${body} headers=${headers} //This is One Response for POST Method
${getuserresponse}= GET On Session mysession /user headers=${headers} //This is 2nd response for GET method which return all the users
FOR ${i} IN #{getuserresponse.json()}
#Validation
IF ${i['firstName']} == ${random_string} // I want to check weather GET Response contains email that I send from POST request
Log User Created Successfully
ELSE
Log User Not Created Successfully
END
Instead it gives me Error as
Evaluating IF condition failed: Evaluating expression 'ptrmipuy'(this is random_string) failed: NameError: name 'ptrmipuy' is not defined nor importable as module
Your conditions cannot have sequences with two or more spaces, since that's what robot uses to parse a statement. Everywhere you have == it needs to be ==
Also, your expressions either need to quote the string values or you can use the special syntax that converts the robot variables into python variables.
IF "${i['firstName']}" == "${random_string}"
-or-
IF $i['firstName'] == $random_string
This is covered in the documentation for the BuiltIn library, in a section titled Evaluating Expressions
Try below snippet of if else, if it works then copy paste this syntax and replace your variable. Also print your variable before comparing, if it has extra quotes, then you might get that error.
Also you should have only space before and after this symbol "=="
*** Test Cases ***
Testifelse
${Variation}= Set Variable NA
IF "${Variation}" == "NA"
Log if
ELSE
Log else
END
I store in database a string with concatenated variables but when I fetch it, it behaves like string, and the variable values are not reflected.
Stored in database field I have:
"""\
Please visit the following link to grant or revoke your consent:
"""+os.environ.get("PROTOCOL")+"""://"""+os.environ.get("DOMAIN")+"""/consent?id="""+consentHash+""""""
I need to be able to fetch it in python and store it in a variable but have the concatenated variable values reflected:
someVariable = database['field']
But like this the concatenated variable values are not processed and the whole thing behaves like one string.
When I print(someVariable) I am expecting
Please visit the following link to grant or revoke your consent:
https://somedomain/consent?id=123
But instead I get the original stored string as in database field:
"""\
Please visit the following link to grant or revoke your consent:
"""+os.environ.get("PROTOCOL")+"""://"""+os.environ.get("DOMAIN")+"""/consent?id="""+consentHash+""""""
You can call eval on your string to have it, uh, evaluate the string as an expression.
Using eval is considered dangerous, because it can be used to do pretty much anything you could write code for, without knowing just what that code will be ahead of time. This is more of an issue when using it on strings provided from an outside source.
Summary
I am attempting to concatenate the output of json.dumps - converting part dictionary variable, that has been passed from a client to a server, to a string - with a string, before passing it to a variable where it defines the name of a mbox file to be accessed.
Variables defined
The below variables are defined in the server's code.
recp_encoded = receive_message(client_socket)
recp = format(recp_encoded['data'].decode('utf-8'))
Variables accessed
json_user = (json.dumps(recp) + '.mbox')
print(json_user)
mailbox_name = str(json_user)
mbox = mailbox.mbox(mailbox_name)
mbox.lock()
However, the above code does not work as it formats the string as:
"user2".mbox'
Rather than the:
'user2.mbox'
That I need. In short, how do I fix the above to format the string to how I need it?
It seems that recp is a simple string type. Why not use the variable as given?
json_user = recp_encoded['data'].decode('utf-8') + '.mbox'
Will this not give you want you need? It doesn't seem that recp is sent as a JSON.
If I pass the string ${varName} to the built in keyword Log to Console, the console outputs the literal string ${varName}. If there is a variable named varName with a value of test123, how do I get the keyword Log to Console to output the variable value of test123 when I pass in ${varName}?
I'm making a data-driven script from an Excel spreadsheet. In the string values I'm passing in, there are variables names within it that I want replaced with the variable's value.
I've tried to run the string through the Evaluate keyword, but it just changes all of the variable names like ${varName} to RF_VAR_varName, so it's recognizing something here?
Open Excel ${ExcelFile}
${varName} Read Cell Data By Coordinates ${Sheet_Name} 0 ${RowNum}
set global variable ${varName}
log varName: ${varName} console=yes
Would output to the console:
'38773461|${TMS_ConfNo}|substr:RDSJUMHV FIRSTNAME|${globalLastName} FIRSTNAME|fullline:JEYCTINY, FIRSTNAME|${globalLastName1}, FIRSTNAME|fullline:RDSJUMHV, FIRSTNAME|${globalLastName}, FIRSTNAME|fullline'
I would like this string:
'38773461|${TMS_ConfNo}|substr:RDSJUMHV FIRSTNAME|${globalLastName} FIRSTNAME|fullline:JEYCTINY, FIRSTNAME|${globalLastName1}, FIRSTNAME|fullline:RDSJUMHV, FIRSTNAME|${globalLastName}, FIRSTNAME|fullline'
To evaluate into this:
'38773461|12345678|substr:RDSJUMHV FIRSTNAME|LASTNAME FIRSTNAME|fullline:JEYCTINY, FIRSTNAME|LASTNAME1, FIRSTNAME|fullline:RDSJUMHV, FIRSTNAME|LASTNAME, FIRSTNAME|fullline'
I would like all variables in the string to translate to their respective value.
Robot has a built-in keyword named Replace variables which will replace variables with their values in a string.
I've been trying to use the faker library to generate data without having it in my test cases as static data.
I have tried calling fake.md5(raw_output=False) directly from my keyword and also by creating a variable and assigning it this value, but neither has the intended effect. It seems that no matter what I do, the only output I'm getting during my test is fake.md5(raw_output=False).
What am I doing wrong?
Edit: My keyword (it writes to a specific field, this is just a test keyword to make sure I can use faker) -
Write username
${md5}= MD 5
${my data}= log md5: ${md5}
Input Text a11y-username ${my data}
Edit #2 - I realized I had missed out the log keyword, I have updated my code
The problem is in this statement:
${my data}= md5: ${md5}
Robot expects the first cell (or the first cell after a variable name) to be a keyword. So, in this case it thinks md5: ${md5} is a keyword, which it obviously is not. That is why you get the error No keyword with name 'md5: ${md5}' found.
I don't know what you're expecting to do with that line of code. Your value is already in a variable, are you trying to copy it to another variable, or simply print it out?
If your intention was to log the value, use the Log keyword:
Write username
${md5}= MD 5
log md5: ${md5}
If you instead want to copy the value to another variable, you can use the Set Variable keyword:
write username
${md5}= MD 5
${my data}= set variable ${md5}
Input Text a11y-username ${my data}