I have a list that might return null values, then I have to iterate through it...ex:
for iter_aeh in range(len(alert_event_history_rows)):
alert_event_history_rows is a list and it can be null, so I want to set len(alert_event_history_rows) = 1 if alert_event_history_rows is a list of null values(0 rows).
But I get following error:
SyntaxError: can't assign to function call
for record_ae in alert_event_rows:
if len(alert_event_history_rows) == 0:
len(alert_event_history_rows) = 1
for iter_aeh in range(len(alert_event_history_rows)):
How to set the value of len(alert_event_history_rows)...?
Don't. Just handle the case where your list is empty with logic:
if alert_event_history_rows:
for item in alert_event_history_rows:
# do something
else:
# alert_event_history_rows was an empty list..
# do something else
This is a syntax error indeed:
len(alert_event_history_rows) = 1
... because you cannot make an assignment to a function call on the left hand side. It must be a variable that can receive the value. If what you really want to do is reinitialize that list to a single element null list, then you might do:
alert_event_history_rows = [None]
This should fix your problem
for record_ae in alert_event_rows:
for iter_aeh in range(max(len(alert_event_history_rows), 1)):
Related
I need to find class="a-icon a-icon-addon couponBadge" on Amazon page, like this.
If the class exist, i need to set variable x == 0, if it doesn't exist, i need to set variable x == 1.
I tried this code, but it doesn't work. How can i check this?
try:
couponBadge = driver.find_elements_by_class_name("a-icon a-icon-addon couponBadge")
if couponBadge != None:
x == 0
else:
x == 1
except AttributeError:
print("error")
Please try this:
couponBadge = driver.find_elements_by_class_name("a-icon a-icon-addon couponBadge")
if couponBadge:
x = 0
else:
x = 1
driver.find_elements_by_class_name returns a list.
In case there are elements matching this locator the list will be non-empty, so it will be interpreted as a Boolean True. Otherwise it will be interpreted as False.
Also to assign a value to variable you should use a single =, not ==.
== is used for comparison.
Also, as mentioned by CrunchyBox you should rather use find_element_by_css_selector, not get_elements_by_class_name.
You need to use a single equals sign, not two, to assign to a variable.
e.g. x = 1
It also looks like Selenium's get_elements_by_class_name function only supports inputting a single class name, not multiple as is shown in your example, so you might want to look into driver.find_element_by_css_selector().
e.g. driver.find_element_by_css_selector('.a-icon.a-icon-addon.couponBadge')
In Java ByteCode there is an opcode called "istore_1", which stores the top value of the stack into index 1 of the local variables, a list. I am trying to replicate this in python, but if you set index 1 of an empty list, its gonna set index 0 rather than index 1. My idea was to check if the first index of the list is empty, and if it is set it to like "emptyindex" or something, but after I did some research, I didnt find a way to check if an index is empty. My question is now how to store a value into index 1 of a list, even if index 0 hasnt been set, and set index 0 to "emptyindex" as a placeholder. Thanks a lot :D
local_variables = []
stack = [1]
user = input("Enter instruction")
if user == "istore_1":
local_variables.insert(1, stack[0])
print(local_variables)
You can use a function to manipulate your list:
def expand(a_list, index, value, empty=None):
l = len(a_list)
if index >= l:
a_list.extend([empty]*(index + 1 - l))
a_list[index] = value
local_variables = []
expand(local_variables, 1, 'str')
print(local_variables)
Output:
[None, 'str']
In Java bytecode, method headers contain a field which gives the maximum size of the local variable table that method uses. So you can just predeclare a list like [None] * MAX_LOCALS. Or you can just do [None] * 65535, since that's the maximum possible local variable table size. Or you can just use a dictionary so you don't have to worry about unset indexes entirely.
I have list like below.
test = ['firstvalue', 'thirdvalue']
I want to insert the some values to the list.
secondvalue at index 1 and fourthvalue at index 3
so the output list looks like below
test = ['firstvalue', 'secondvalue', 'thirdvalue', 'fourthvalue']
I tried the below way but it doesn't work for me
print test.insert(1, "secondvalue")
Is there any alternate way to do this?
The insert function does not return a value, but rather modifies the array used on it. Here's an example:
test = ['firstvalue', 'thirdvalue']
test.insert(1, "secondvalue")
print test
This has taken me over a day of trial and error. I am trying to keep a dictionary of queries and their respective matches in a search. My problem is that there can be one or more matches. My current solution is:
match5[query_site] will already have the first match but if it finds another match it will append it using the code below.
temp5=[] #temporary variable to create array
if isinstance(match5[query_site],list): #check if already a list
temp5.extend(match5[query_site])
temp5.append(match_site)
else:
temp5.append(match5[query_site])
match5[query_site]=temp5 #add new location
That if statement is literally to prevent extend converting my str element into an array of letters. If I try to initialize the first match as a single element array I get None if I try to directly append. I feel like there should be a more pythonic method to achieve this without a temporary variable and conditional statement.
Update: Here is an example of my output when it works
5'flank: ['8_73793824', '6_133347883', '4_167491131', '18_535703', '14_48370386']
3'flank: X_11731384
There's 5 matches for my "5'flank" and only 1 match for my "3'flank".
So what about this:
if query_site not in match5: # here for the first time
match5[query_site] = [match_site]
elif isinstance(match5[query_site], str): # was already here, a single occurrence
match5[query_site] = [match5[query_site], match_site] # make it a list of strings
else: # already a list, so just append
match5[query_site].append(match_site)
I like using setdefault() for cases like this.
temp5 = match5.setdefault(query_site, [])
temp5.append(match_site)
It's sort of like get() in that it returns an existing value if the key exists but you can provide a default value. The difference is that if the key doesn't exist already setdefault inserts the default value into the dict.
This is all you need to do
if query_site not in match5:
match5[query_site] = []
temp5 = match5[query_site]
temp5.append(match_site)
You could also do
temp5 = match5.setdefault(query_site, [])
temp5.append(match_site)
Assuming match5 is a dictionary, what about this:
if query_site not in match5: # first match ever
match5[query_site] = [match_site]
else: # entry already there, just append
match5[query_site].append(temp5)
Make the entries of the dictionary to be always a list, and just append to it.
I'm looking to do something like this:
u = User.objects.filter(name__isnull=True)[0]
The default value in MySQL for name is None. I also tried:
u = User.objects.filter(name=None)[0]
Neither have returned anything, aka, IndexError: list index out of range. How do I get an object with no value set as the name?
Even thought MySQL says the default value is None, Django used "" as the default, so I used:
User.objects.filter(name="").first()
You are missing some key concepts.
IndexError: list index out of range
This error is not a error looking in the db, this error is given because you are trying to do something with an index of array which does not exist.
a = [1,2,3]
a[0] = 1
a[2] = 2
a[3] = 3
a[4] = IndexError: list index out of range
a[n] = IndexError: list index out of range
you can do normaly:
u = User.objects.filter(name=None)
In the DB if you set Null = True and blank = True (i don't recommend this) in the models you can have 'name=None' and 'name=""'
The problem is you are supposing it must be at least one User with your params, for that you are adding the [0] to retrieve a user instance instead a queryset.
If you expect to retrieve only and only one item of the query you must use the .get, (normally used searching pk, if more than one item is returned it gives an error)
u = User.objects.get(name=None)
but, if you know more than one item can exist in your with the filters, (for instance, the name not the pk) and you only care about the first you use the .first method, and later you check if exist.
User.objects.filter(name="").first()