0

Is it possible to split variables that have already been assigned values, and re-piece them back together to hold those same previous values?

For Example:

URLs.QA.Signin = 'https://qa.test.com'
TestEnvironment = 'QA'
CurrentURL = 'URLs.' + TestEnvironment + '.Signin'
print(CurrentURL)

Outputs as: 'URLs.QA.Signin'

but I would like it to:

Output as: 'https://qa.test.com'

The purpose is so I can plug in any value to my 'TestEnvironment' variable and thus access any of my massive list of URL's with ease =P

I am green with Python. Your time and efforts are greatly appreciated! =)


Based upon evanrelf's answer, I tried and loved the following code!:

This is exactly what i'm looking for, I might be over complicating it, any suggestions to clean up the code?

urls = {}
environment = 'qa'
district = 'pleasanthill'
url = environment + district
urls[url] = 'https://' + environment + '.' + district + '.test.com'
print(urls[url])

Output is: https://qa.pleasanthill.test.com

Zero Piraeus
  • 56,143
  • 27
  • 150
  • 160
bitShredder
  • 132
  • 1
  • 11
  • 1
    Possible duplicate of [How do I create a variable number of variables?](https://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables) – Ilja Everilä Dec 09 '17 at 08:42
  • Hi llja - So I looked at the potential duplicate: "How do I create a variable number of variables" and it could very well be a duplicate. But for me it is difficult to understand that post. I think if nothing else this post is a bit more simplistic for those who are beginners. Your thoughts? =) (I appreciate your due dilligence to keep the posts nice and clean! =P) – bitShredder Dec 09 '17 at 08:50

2 Answers2

1

I would recommend you look into Python's dictionaries.

urls = {}
urls['qa'] = 'https://qa.test.com'

test_environment = 'qa'
print(urls[test_environment])
// => https://qa.test.com
evanrelf
  • 303
  • 3
  • 12
  • Thank you so much for the example, and the reference to learn dictionaries, I have been staring at and testing dictionary code for the last few hours! =) – bitShredder Dec 09 '17 at 08:33
  • You're welcome! I don't know exactly how you're organizing these URLs, but you might also want to check out nested dictionaries (a dictionary within a dictionary). That way you could say something like `urls['qa']['pleasanthill']`. https://stackoverflow.com/a/16333441/1664444 – evanrelf Dec 09 '17 at 17:04
0

I believe to my comprehension that you are trying to input a string and get a new string (the url) back. The simplest answer that I can understand is to use a dictionary. An example of this is by simply doing

URLS = {'sheep' : 'wool.com', 'cows' : 'beef.com'}

either this or by using two arrays and referencing a common index, but who wants to do that :p

  • I love this idea as well, this is certainly getting me thinking! =) (Yeah no kidding referenceing index's can get confusing really fast LOL =P) – bitShredder Dec 09 '17 at 08:33