What does this python codes mean? - python

What does this python codes mean? New to python. THX!
benchmark_sets_list = [
'%s: %s' %
(set_name, benchmark_sets.BENCHMARK_SETS[set_name]['message'])
for set_name in benchmark_sets.BENCHMARK_SETS]

This part...
for set_name in benchmark_sets.BENCHMARK_SETS
...will grab the set names from benchmark_sets.BENCHMARK_SETS and it will keep them one by one into a set_name variable.
After that, it will be able to know the values from this line...
(set_name, benchmark_sets.BENCHMARK_SETS[set_name]['message'])
...because set_name will have a value. That part will return two things, set_name and benchmark_sets.BENCHMARK_SETS[set_name]['message']. Probably those two things will be both strings.
Then, those %s you see in this line...
'%s: %s' %
...will be replaced by the value of set_name and benchmark_sets.BENCHMARK_SETS[set_name]['message'] respectively. That will generate an string like this one: "foo: bar", being "foo" the value of set_name and "bar" the value of benchmark_sets.BENCHMARK_SETS[set_name]['message'].
In order for you to understand what happened there, this is a simple example:
"%s %s %s" % (first_elem, second_elem, third_elem)
That code will replace the first %s with the value of first_elem The second %s with the value of second_elem, and the third %s with the value of third_elem.
And finally that string will be added to the list which is being constructed. So, at the end you will have an list more or less like this one:
["foo: bar", "wop: wap", "bing: bang"]

Related

Can i put anything in for x

I am learning how to code in python so I'm wondering if we can put anything in for x (key in this instance):
for key in prices:
print key
print "price: %s" % prices[key]
print "stock: %s" % stock[key]
Yes, you can put any name there that you wish -- just as long as it's a valid variable name in Python. key is going to be the variable name for each item inside the for block.

Using %s within a Selenium method

Right now I have the method below, it works perfectly fine but I want to change part of the self.wait_for_element(....) to instead use %s rather than calling str(counter)
> def gather_recent_visited_courses_in_content_switcher(self):
hover_courses = self.browser.find_elements_by_xpath("//div[contains(#class, 'recent-content flyout-closed')]")
course_list = []
counter = 1
for course in hover_courses:
self.hover_over(course)
# Change the below to %s
self.wait_for_element("//div[contains(#class, 'fly-wrapper recent-content-trigger')][" + str(counter) + "]//div[contains(#class, 'recent-content-info')]", 'Course list not found')
course_display_name = course.find_element_by_xpath("//div[contains(#class, 'recent-content-info')]").text
course_list.append(str(course_display_name))
counter += 1
return course_list
Currently I keep getting errors when replacing it with [%s], like below
> self.wait_for_element("//div[contains(#class, 'fly-wrapper recent-content-trigger')][%s]//div[contains(#class, 'recent-content-info')]", 'Course list not found' %(counter))
Does anyone have any ideas on how to get this to work properly? So far, I keep getting 'not all arguments converted during string formatting' errors
The reason why using %s isn't working is because you are setting the placeholder value in the second string argument, and not the first string as you have intended.
With the first argument:
"//div[contains(#class, ...)][%s]//div[... 'recent-content-info')]"
Python can't find the proper value to replace %s with in the first string argument. So, that will raise an error.
As for the second argument:
'Course list not found' % (counter)
You are passing a value to the string but the string cannot be formatted to use the passed value because the string doesn't have placeholder, %s. So, that'll raise an error too.
To fix that, just format the first string argument. It'll look like this:
"//div[contains(#class, '...')][%s]//div[..., 'recent-content-info')]" % counter
Alternatively, you can use .format(). This the new style of string formatting. Using %s is considered to be the old style[1].
"//div[contains(#class, '...')][{}]//div[..., 'recent-co...')]".format(counter)
NOTE: Strings have been redacted to make things easier to read.
References
[1] - https://pyformat.info/

What's the difference between using a variable or %s in Python string formatting?

What is the difference between defining a variable and using it in a string and putting %s in a string and passing in the value after?
site = "Stackoverflow"
site + " is great!"
"%s is great!" % "Stackoverflow"
Printing either of these gives the same result so when is it better to use one over the other?
If you want to keep certain string constants in the same file, or on the top most of the file, you can declare the string with the placeholders as constants, then replace the placeholders with actual variable at run time through the % syntax.
This also allows greater re-usability.
Eg. you can store a single constant "%s is %s years old".
Using this syntax might also make the string more readable.
For two strings, there is little difference.
For multiple strings, s1 + s2 + s3 is less efficient, as it has to create a temporary str object for the first concatenation, where as both "%s %s %s" % (s1, s2, s3) and "{} {} {}".format(s1, s2, s3) creates the final str object immediately.
One:
'string' + 'string'
Two:
'%s %s' % ('one', 'two')
'{} {}'.format('one', 'two')
There is a great article on this here: https://pyformat.info/
Also the docs are a great resource: https://docs.python.org/2/library/string.html#format-string-syntax
Version one is less efficient with larger amounts of concatenation.

Output formatting in Python: replacing %s with the defined variable

How do I replace %s with a defined/non-empty variable string? Or rather what is the Pythonic or syntactic sugar for doing so?
Example:
# Replace %s with the value if defined by either vehicle.get('car') or vehicle.get('truck')
# Assumes only one of these values can be empty at any given time
# The get function operates like http://docs.python.org/2/library/xml.etree.elementtree.html#xml.etree.ElementTree.Element.get
logging.error("Found duplicate entry with %s", vehicle.get('car') or vehicle.get('truck'))
I think you want this:
'Found duplicate entry with %s' % (vehicle.get('car') or vehicle.get('truck'))
This will replace the '%s' with the non-empty string (assuming only one is non-empty). If both contain text, it will be replaced with the output of vehicle.get('car')
You could also use this type of string formatting:
'Found duplicate entry with {0}'.format(vehicle.get('car') or vehicle.get('truck'))
This will return the same result.
Have you tried something like this?
logging.error("Found duplicate entry with %s" % (vehicle.get('car') or vehicle.get('truck')))
Or if truck is also empty, you can return a default value:
logging.error("Found duplicate entry with %s" % (vehicle.get('car') or vehicle.get('truck', 'default')))

how to replace %s with one variable in python

I have a string like
strTemp='i don\'t want %s repeat the %s variable again %s and again %s'%('aa','aa','aa','aa')
I want to replace all the %s with 'aa', so I have to repeat the 'aa' for many times, how can I tell the program that I want to replace all the %s just with the same variable, so I needn't type the variable for times
The % operator is discouraged. Use str.format instead.
strTemp='i don\'t want {0} repeat the {0} variable again {0} and again {0}'.format('aa')
You could use the named formatting argument syntax:
strTemp='i don\'t want %(key)s repeat the %(key)s variable again %(key)s and again %(key)s' % {
'key': 'replacement value',
}
Not a lot better necessarily since you have to repeat a key four times, but if your replacement value is long, or is a calculation with side effects that you really don't want to do more than once, this is a good option.
if you have really many repetitions and don't want to type {0} or %(var)s you may consider a seldom used (placeholder-)char, which gets replaced, e.g.:
p='µ µµ µµµ µµµµ µµµµµ {1}'.replace('µ','{0}').format(' muahaha ', 'duh')
or for a single substition variable:
strTemp='i don\'t want µ repeat the µ variable again µ and again µ'.replace('µ','aa')

Categories