elif token[1] == 'in' and not (isinstance(token[2], Query) or token[2]):
Exception
IndexError: string index out of range
This usually happens when we forget to wrap our search parameters into a tuple inside the list, like so:
self.search(['field', '=', value])
While it should be:
self.search([('field', '=', value)])
Note the ( before 'field', and the ) after value.
It's not a Odoo error it's simply a Python error.
Your variable token is a string but by looking at your code token must be a list or tuple.
Related
I have to variables one="{}{}{}T{}:{}:{}" and two='2021,-10,-28,05,40,33' When i try to use print(one.format(two)) i am getting errors. IndexError: Replacement index 1 out of range for positional args tuple. guessing it is because the two variable is being seen as a string.
So I switched and tried to do the *args method.
for item in two:
item = str(item)
data[item] = ''
result = template.format(*data)
However if I switch two='2021,-10,-28,00,00,00.478' it fails because a dictionary is unique. so how do i get the first method to work or is there a better solution.
You should split the two into list and then unpack it with *
one="{}{}{}T{}:{}:{}"
two='2021,-10,-28,05,40,33'
print(one.format(*two.split(','))) # 2021-10-28T05:40:33
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()
In python i have this code
if record[0][1]:
the problem is.. when mysql does not return anything and thus..
record[0][1]
has no data..
this python code fails:
if record[0][1]:
IndexError: tuple index out of range
i simply want it to move on to the "else" statement or simply consider this if statement as .. invalid given that
record[0][1]
has no value. or data.. ( incoming stuff from mysql )
try:
if record[0][1]:
# Do stuff
except IndexError:
pass
You can use a try...except or use a short-circuiting check on outer tuple.
if len(record) > 0 and len(record[0]) > 1 and record[0][1]:
I'm trying to debug some Python 2.7.3 code to loop through a list of items and convert each to a string:
req_appliances = ['9087000CD', 'Olympus', 185]
for i in range(0, len(req_appliances)):
req_appliances[i] = str(req_appliances[i])
print req_appliances
The output is as follows:
['9087000CD', 'Olympus', '185']
In the example above, I've set the value of req_appliances explicitly to test the loop. In the actual code, req_appliances is an argument to a function. I do not know the type of the argument at runtime, but it appears to be a list of scalar values. I do know that when I invoke the function, I see the following error message:
File ".../database.py", line 8277, in get_report_appliance_list
req_appliances[i] = str(req_appliances[i])
TypeError: 'str' object does not support item assignment
I'm trying to deduce for what values of argument req_appliances it would be possible for this error condition to arise. It seems to me that all of the values are scalar and each (even if immutable) should be a valid LHS expressions in an assignment. Is there something I'm missing here? Here is the code in context, in the function in which it is defined:
def get_report_appliance_list(self, req_appliances, filter_type=None):
appliances = {}
appliance_list = []
if filter_type != None:
if filter_type not in ('appliances', 'servers'):
raise ValueError("appliance filter_type must be one of 'appliances' or 'servers'")
active_con = self.get_con()
if active_con is None:
raise Exception('No database connections are available.')
con = None
in_expr_items = ''
if req_appliances != None:
# Create a string like '(%s, %s, ...)' to represent
# the 'in' expression items in the SQL.
print(req_appliances)
for i in range(0, len(req_appliances)):
req_appliances[i] = str(req_appliances[i])
in_expr_items += '%s,'
in_expr_items = '(' + in_expr_items[:-1] + ') '
An str acts like a sequence type (you can iterate over it), but strings in Python are immutable, so you can't assign new values to any of the indices.
I expect what's happening here is that you're trying to run this when req_appliances is a str object.
I came up with two ways to fix this:
First, just check if it's a str before you iterate over it:
if isinstance(req_appliances, basestring):
return req_appliances
Second, you could check each item to see if it's already a string before trying to assign to it.
req_appliances = ['9087000CD', 'Olympus', 185]
for i in range(0, len(req_appliances)):
if req_appliances[i] != str(req_appliances[i]):
req_appliances[i] = str(req_appliances[i])
print req_appliances
Here, I'm actually checking whether the member is equal to its string representation. This is true when you iterate over strings.
>>> a = 'a'
>>> a[0] == str(a[0])
True
This is not really an answer to your question, but a style advise. If you happen to use for i in range(0, len(something)) a lot you should either use for i, obj in enumerate(something), map(func, something) or a list comprehension [func(x) for x in something].
Another red flag is the use of string += inside a loop. Better create an array and join it. This also eliminates the need to do stuff like [-1] in order to get rid of trailing commas.
Regarding your code you could simplify it a lot:
def get_report_appliance_list(self, req_appliances, filter_type=None):
appliances = {}
appliance_list = []
if filter_type not in (None, 'appliances', 'servers'):
raise ValueError("appliance filter_type must be one of 'appliances' or 'servers'")
active_con = self.get_con()
if active_con is None:
raise Exception('No database connections are available.')
# Create a string like '(%s, %s, ...)' to represent
# the 'in' expression items in the SQL.
in_expr_items = ','.join(['%s'] * len(req_appliances)
req_appliances = map(str, req_appliances)
...
Apart from that I would recommend that get_con() throws so you do not have to check for None in your code.
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)):