How to evaluate string as variable name - python

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.

Related

Python set problems

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

Trouble printing variables from other files

Description
I have a script → report_creator.py which will need some variables from another python script → extracted_data.py.
The number of variables from extracted_data.py will always be based on the system it extracts data from (if the system has 1 server, we will have 1 variable, if the system has 2 servers, we will have 2 variables and so on). You can never know the exact number of variables the extracted_data.py will have.
Let's say we have extracted data from a system with 2 servers and the extracted_data.py file looks something like:
parameterx_server1 = "value"
parameterx_server2 = "another value"
What I have: → report_creator.py:
import os
import extracted_data
#Extract all variables names starting with parameterx from extracted_data.py and store them into a list
variables = os.popen("""cat extracted_data.py | grep '^parameterx_*' | awk '{print $1}'""").read().strip()
variables = variables.split("\n")
#After the execution of the command, the list looks like this:
# variables = ['parameterx_server1', 'parameterx_server2']
Problem
The script now has a list containing all the parameterx variables from extracted_data.py:
variables = ['parameterx_server1', 'parameterx_server2']
The only thing remaining is to get the corresponding value of each variable from the variables list from the extracted_data.py, something like:
print extracted_data.parameterx_server1
I tried something like:
for variable in variables:
print extracted_data.variable
But for some reason I get an AttributeError: 'module' object has no attribute 'variable'.
You can extract the variables and their value defined in the extracted_data.py file like this:
import extracted_data
extract_vars = {name: getattr(extracted_data, name)
for name in dir(extracted_data) if not name.startswith('_')}
print(extract_vars) # -> {'parameterx_server2': 'another value', 'parameterx_server1': 'value'}
As shown, afterwards extract_vars is a dictionary containing both the variable names and associated values.

How to concatenate a json.dumps output with a string

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.

Use dictionary as lookup table

I have a jinja template that I want to pass a value into (an identifier for a country). The format of the data is a two letter country code (for example "PL" for Poland).
Through the template, I need to pass the corresponding flag as an output, but the flag is saved in the app folder structure, so i need to get the path to the image.
My problem: I could not figure out a way to use os.path in jinja, so now im trying to solve it by creating a dictionary that matches country string and relative path like this:
countries = {"PL" : "countries/flags/poland.png"}
where the system path to the app folder gets added afterwards in Python through os.path.
My question: How can i use the country string I am getting to automate transformation into the path format of the country? Something like:
for data in countries:
if data in countries.keys:
return countries.value
Thanks in advance!
Assuming data is a country code (like "PL" for example):
def get_path(data):
return countries.get(data)
The get() method checks if the dictionary has the key, and if so returns the corresponding value, otherwise it returns None.
If you want a default value other than None when the key is not present you can specify it as second argument, like this:
def get_path(data):
return countries.get(data, "default/path/x/y/z")

How can I use faker in conjunction with Robot Framework?

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}

Categories

Resources