I'm trying to read a dictionary in my python script from an environment variable.
This is the code of my python script:
desired_cap_default = [
{'platform': 'Windows 7', 'browserName': 'firefox', 'version': '24.0'},
{'platform': 'OS X 10.10', 'browserName': 'chrome', 'version': '45.0'},
{'platform': 'Windows XP', 'browserName': 'chrome', 'version': '40.0'},
{'platform': 'OS X 10.10', 'browserName': 'safari', 'version': '8.0'},
# {'platform': 'Windows XP', 'browserName': 'firefox', 'version': '10.0', 'screenResolution': '1600x1200',
# 'videoUploadOnPass': False, 'commandTimeout': 120}
]
browser = os.getenv('TESTING_BROWSERS', desired_cap_default)
And this is how I'm specifying the environment variable (but is not recognising it as a dictionary)
TESTING_BROWSERS="[{'platform': 'Windows 7', 'browserName': 'firefox', 'version': '24.0'}, \
{'platform': 'OS X 10.10', 'browserName': 'chrome', 'version': '45.0'}, \
{'platform': 'Windows XP', 'browserName': 'chrome', 'version': '40.0'}, \
{'platform': 'OS X 10.10', 'browserName': 'safari', 'version': '8.0'}, ]"
Can anyone help me figuring out what am I missing? Thanks in advance guys
You can you use the ast
module for this.
TESTING_BROWSERS = "[{'platform': 'Windows 7', 'browserName': 'firefox', 'version': '24.0'}, \
{'platform': 'OS X 10.10', 'browserName': 'chrome', 'version': '45.0'}, \
{'platform': 'Windows XP', 'browserName': 'chrome', 'version': '40.0'}, \
{'platform': 'OS X 10.10', 'browserName': 'safari', 'version': '8.0'}, ]"
import ast
my_dict = ast.literal_eval(TESTING_BROWSERS)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With