-1

Latest update and explanation: For those who really don't understand why this was being done, I was using an Excel API in Python. The Python script would pull strings from the Excel spreadsheet for automated tests and return results to Excel when completed. I would also update a database, but have since quit the idea, and the need for Excel on this project, due to the difficulties we have seen in the discussions.

For example, I understand you can...

my_string = "driver.find_element_by_id(%s).click()" % foo

but how can I apply the data to an already assigned string. What would be the Pythonic way to...

my_string = 'driver.find_element_by_id(%s).click()'

new_string = my_string % foo

UPDADED: Playing with quotes got me sidetracked. Not exactly what I want on the final product but this is what now works...

these str objects pulled in from another source (so no way to join before assigning to variables)...

a = "driver.find_element_by_id('%s').click()"
sel_selector = "foo"

my_string = a % sel_selector
eval(my_string)

Most of my logic of capturing the data from an Excel spreadsheet has been left out, but that is why it became so complicated. It is a script that pulls data from a spreadsheet, so we can have automated regression testing that updates the sheet.

Shane
  • 1,629
  • 3
  • 23
  • 50
  • 1
    Your first line isn't valid - so it doesn't serve the purpose of explaining what you're trying to do. What do you mean by "converted"? – Lukas Graf Jul 11 '14 at 18:58
  • I added the quotes back. – Shane Jul 11 '14 at 19:08
  • Still not valid, you might want to use double quotes. But my actual point was that you should explain what you want to do, in english, because it isn't clear from your code samples. – Lukas Graf Jul 11 '14 at 19:11
  • you cannot have single quotes inside a string wrapped in single quotes. Why is the command in a string in the first place? – Padraic Cunningham Jul 11 '14 at 19:15
  • @PadraicCunningham I actually changed it really fast without looking because I am looking at the only answer and his format() option. Unfortunately, did not think about the double quotes issue initially. – Shane Jul 11 '14 at 19:23
  • what are you trying to achieve exactly? – Padraic Cunningham Jul 11 '14 at 19:24
  • Basically I want to allow a set of commands for WebDriver and to allow custom id and class selectors to be injected into the command. – Shane Jul 11 '14 at 19:25
  • Also, I can save the command as `my_string = driver.find_element_by_id("foo").click()` and it works. I just need to find a way to combine them at run time. – Shane Jul 11 '14 at 19:28
  • @Shane, see my updated answer, I should have what you want – wnnmaw Jul 11 '14 at 19:33
  • how are the custom id and class selectors passed in? – Padraic Cunningham Jul 11 '14 at 19:43
  • @wnnmaw your code works, I am just trying to figure out some of the other details. – Shane Jul 11 '14 at 19:53
  • I was having issues when the driver command is converted to a string. so basically I am mixing in a lot of blockers for what I wanted to achieve, so it requires a bit more work than expected. I can now at least use eval() if I want to build the driver command, then convert from string back into a method. Either way, your help was great! – Shane Jul 11 '14 at 20:16
  • why are you storing it as a string anyway? – Padraic Cunningham Jul 11 '14 at 20:25

1 Answers1

1

With the format() function, you can do exactly that, but I have no idea if its compatible with whatever functions you're using:

>>> a = "hello {}"
>>> a
'hello {}'
>>> a.format("dave")
'hello dave'

It also works the the % formatting, but I think its less pretty :)

>>> a = "hello %s"
>>> a
'hello %s'
>>> a % "dave"
'hello dave'

After reading your comments, I you can do what you want with exec as shown in the example below:

>>> command = "lst = [i**2 for i in range({})]"
>>> exec command.format(10)  #Performs identically to lst = [i**2 for i in range(10)]
>>> lst
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
>>> exec command.format(4)   #Performs identically to lst = [i**2 for i in range(4)]
>>> lst
[0, 1, 4, 9]

See this post for a complete explanation.

Community
  • 1
  • 1
wnnmaw
  • 5,444
  • 3
  • 38
  • 63
  • Btw, my first attempts at passing the `%` was working, but the issues with attempting to pass into a method was the issue. Spending time on your options helped me understand the logic better. – Shane Jul 11 '14 at 22:25