How to tackle a NoneTypeError? - python

I have a function which returns a list. But it has nothing to return. I want to handle the TypeError.
I have tried this:
def isLookingAround(lst):
res = []
body = []
for data in lst:
res += isLookingAt(normalize2(data))
body += isBodyDirection(normalize2(data))
if most_body == "front" or most_body == "backward":
if ('lookL' in res and 'lookR' in res):
return 'lookingAround'
elif most_body == "left" or most_body == "right":
if ('lookF' in res and 'lookB' in res):
return 'lookingAround'
Error:
Traceback (most recent call last):
File "action_detector.py", line 201, in <module>
write_labels(input_source, labels)
File "action_detector.py", line 179, in write_labels
for itr, word in enumerate(lbls):
TypeError: 'NoneType' object is not iterable
I am still getting the error with labels.append(detectors.isLookingAround(back_Data)) . I would appreciate your help.

To check data is empty. you can use below code
if data:
l.append(data)

In order to handle the NoneType exception, you can surround the statement within try-except blocks.
For example, you can do something like,
try:
labels.append(detectors.isLookingAround(back_Data))
except TypeError:
# Do something. Like,
return
It will "catch" the error, and then you can handle it properly. However, the error seems to come from isLookingAround method.
You can re-check that, and if you're unable to resolve it, post a new question, I guess.

Related

TypeError: 'NoneType' object is not subscriptable in a int object

Here is a function to check the data and update it
div , update are my mongodb collection object
def data_updater(user1_id,code):
device_id = dvi.find_one({"user_id":user1_id},{"_id":0,"user_id":0})["device_id"]
prv_data = update.find_one({"device_id":device_id},{"_id":0,"device_id":0})
prv_date = prv_data["date"]
msg = prv_data["message"]
if prv_date < current_date and msg != code:
x = update.find_one_and_update({"device_id":id,},{"$set":message":code,"date":current_date}})
print(x.acknowledged)
and when I am calling the function it is giving TypeError data_updater(95626,972681)
the error
Traceback (most recent call last):
File line 170, in <module>
data_updater(95626,972681)
File line 71, in data_updater
device_id = dvi.find_one({"user_id":int(user1_id)},{"_id":0,"user_id":0})["device_id"]
TypeError: 'NoneType' object is not subscriptable
I am not able to find any mistake please help
Your context isn't very clear, however, from the error trace as generated it seems that your find_one() function returns None with the arguments as passed and you are trying to access the value for the key device_id. I recommend you refactor your find_one() function or make use of the following code to resolve the issue at hand:
def data_updater(user1_id,code):
try:
device_id = dvi.find_one({"user_id":user1_id},{"_id":0,"user_id":0})["device_id"]
prv_data = update.find_one({"device_id":device_id},{"_id":0,"device_id":0})
prv_date = prv_data["date"]
msg = prv_data["message"]
if prv_date < current_date and msg != code:
x = update.find_one_and_update({"device_id":id,},{"message":code,"date":current_date}})
print(x.acknowledged)
except TypeError:
print('No value found for specified parameters :/')
PS: I also didn't understand the use of $ in your code, so I removed it thinking of it as a mistake. Hope this helps! 😊

How to get value from JSON - Python

If anyone can help me to get a value from a json response. I intend to get only the first incidence of id where the type is CLIENTREF.
I am reading a list to get some values to do a GET and check if the response is other than null "".
This is the JSON structure:
This is my code so far
def checkParent(list):
for item in list:
cdwParenturl = f"http://cdwu/cdw/counterparties/{item}/?yyyy-mm-dd={dateinplay}"
r = requests.get(cdwParenturl).json()
jsonpath_expression = parse("$..identifier[*]")
# $..identifier[?(#.type == 'CLIENTREF')].id
parentId = []
for match in jsonpath_expression.find(r):
# print(f"match id: {match.value}")
thisdict = match.value
if thisdict["type"] == "CLIENTREF":
# print(thisdict["id"])
parentId.append(thisdict["id"])
elif thisdict["id"] == "":
print(colored(f"The parent {item} does not have ultimateParent", "red"))
print(colored(f"All counterparties have parent", "green"))
checkParent(r_mheu_trade)
print(f "match id: {match.value}") I get this, What I need is the first id related to the type: CLIENTREF
The error that appears on my console is this:
Traceback (most recent call last):
File "h:\DESKTOP\test_check\checkCounterpartie.py", line 114, in <module>
checkParent(r_mheu_trade)
File "h:\DESKTOP\test_check\checkCounterpartie.py", line 106, in checkParent
if thisdict["type"] == "CLIENTREF":
KeyError: 'type'
With the help of pyzer I found a solution to my problem. This is how the code looks like:
# Request to get all UltimateParent
def checkParent(murex):
for item in murex:
cdwParenturl = f"http://cdwu/cdw/counterparties/{item}/?yyyy-mm-dd={dateinplay}"
r = requests.get(cdwParenturl).json()
clientref = r[0]["riskUltimateParent"]["identifier"][1]
if clientref == "":
print(colored(f"The parent {item} does not have ultimateParent", "red"))
else:
print(colored(f"All counterparties have parent", "green"))
checkParent(r_mheu_trade)

AttributeError: 'tuple' object has no attribute 'status_code'

I'm a beginner in python. I'm not able to understand what the problem is?
the runtime process for the instance running on port 43421 has unexpectedly quit
ERROR 2019-12-24 17:29:10,258 base.py:209] Internal Server Error: /input/
Traceback (most recent call last):
File "/var/www/html/sym_math/google_appengine/lib/django-1.3/django/core/handlers/base.py", line 178, in get_response
response = middleware_method(request, response)
File "/var/www/html/sym_math/google_appengine/lib/django-1.3/django/middleware/common.py", line 94, in process_response
if response.status_code == 404:
AttributeError: 'tuple' object has no attribute 'status_code'
Whatever middleware_method returns is a tuple, so in the form ('a', 1, []) or something.
The error is telling you that you can't access members of the tuple by name, because they don't have names.
Maybe you created a tuple like this:
status_code = 404
name = 'Not found'
response = (name, status_code)
Once you declare the tuple, the names that went into it are lost. You have a couple of options for getting things out.
Direct access
You can get the objects by index, like you would with a list:
assert response[1] == 404
If you don't know what the tuple looks like, just print it, and calculate the index.
Named tuple
If you're determined to use names, you can create a namedtuple, provided that the tuple is going to be in the same format every time.
from collections import namedtuple
Response = namedtuple('Response', ('name', 'status_code')
response = Response('Not found', 404)
assert response.status_code == 404
Alternatively there may be a mistake in your code, where you are inadvertently returning a tuple, but one part of it is a requests.Response object. In that case, you can just extract the object as in "Direct access", and then use as you are already.
Would have to see the code to be of more help, but it might be something like:
response[2].status_code
I will try to explain how this error comes with a simple example
def example_error():
a1 = "I am here"
b1 = "you are there"
c1 = "This is error"
return a1, b1, c1
def call_function():
strings = example_error()
s1 = strings.a1
s2 = strings.b1
s3 = strings.c1
print(s1, s2, s3)
call_function()
This will return the error
AttributeError: 'tuple' object has no attribute 'a1'
Because I have returned three variables a1, b1, c1 in example_error function and trying to get them by using single variable strings.
I can get rid of this by using the following modified call_function
def call_function():
strings = example_error()
s1 = strings[0]
s2 = strings[1]
s3 = strings[2]
print(s1, s2, s3)
call_function()
As you did not show your code, I assume you have done something similar as here in the first case.

sqlalchemy.exc.InvalidRequestError: Can't attach instance another instance with key is already present in this session

Here I have a mistake that I can't find the solution. Please excuse me for the quality of the code, I didn't start classes until 6 months ago. I've tried to detach category objects with expunge but once it's added it doesn't work.I was thinking when detaching the object with expunge it will work. and I can't find a solution :( . I pasted as much code as I could so you could see
Traceback (most recent call last):
File "/home/scwall/PycharmProjects/purebeurre/recovery.py", line 171, in <module>
connection.connect.add(article)
File "/home/scwall/PycharmProjects/purebeurre/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1776, in add
self._save_or_update_state(state)
File "/home/scwall/PycharmProjects/purebeurre/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 1796, in _save_or_update_state
self._save_or_update_impl(st_)
File "/home/scwall/PycharmProjects/purebeurre/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2101, in _save_or_update_impl
self._update_impl(state)
File "/home/scwall/PycharmProjects/purebeurre/venv/lib/python3.6/site-packages/sqlalchemy/orm/session.py", line 2090, in _update_impl
self.identity_map.add(state)
File "/home/scwall/PycharmProjects/purebeurre/venv/lib/python3.6/site-packages/sqlalchemy/orm/identity.py", line 149, in add
orm_util.state_str(state), state.key))
sqlalchemy.exc.InvalidRequestError: Can't attach instance <Categories at 0x7fe8d8000e48>; another instance with key (<class 'packages.databases.models.Categories'>, (26,), None) is already present in this session.
Process finished with exit code 1
class CategoriesQuery(ConnectionQuery):
#classmethod
def get_categories_by_tags(cls, tags_list):
return cls.connection.connect.query(Categories).filter(Categories.id_category.in_(tags_list)).all()
other file:
def function_recovery_and_push(link_page):
count_and_end_page_return_all = {}
count_f = 0
total_count_f = 0
list_article = []
try:
products_dic = requests.get(link_page).json()
if products_dic['count']:
count_f = products_dic['page_size']
if products_dic['count']:
total_count_f = products_dic['count']
if not products_dic['products']:
count_and_end_page_return_all['count'] = False
count_and_end_page_return_all['total_count'] = False
count_and_end_page_return_all['final_page'] = True
else:
count_and_end_page_return_all['final_page'] = False
for product in products_dic["products"]:
if 'nutrition_grades' in product.keys() \
and 'product_name_fr' in product.keys() \
and 'categories_tags' in product.keys() \
and 1 <= len(product['product_name_fr']) <= 100:
try:
list_article.append(
Products(name=product['product_name_fr'], description=product['ingredients_text_fr'],
nutrition_grade=product['nutrition_grades'], shop=product['stores'],
link_http=product['url'],
categories=CategoriesQuery.get_categories_by_tags(product['categories_tags'])))
except KeyError:
continue
count_and_end_page_return_all['count'] = count_f
count_and_end_page_return_all['total_count'] = total_count_f
list_article.append(count_and_end_page_return_all)
return list_article
except:
count_and_end_page_return_all['count'] = False
count_and_end_page_return_all['total_count'] = False
count_and_end_page_return_all['final_page'] = True
list_article.append(count_and_end_page_return_all)
return list_article
p = Pool()
articles_list_all_pool = p.map(function_recovery_and_push, list_page_for_pool)
p.close()
for articles_list_pool in articles_list_all_pool:
for article in articles_list_pool:
if type(article) is dict:
if article['count'] != False and article['total_count'] != False:
count += article['count']
total_count = article['total_count']
if article['final_page'] is True:
final_page = article['final_page']
else:
connection.connect.add(article)
I receive this as an error message, thank you in advance for your answers
This error happens when you try to add an object to a session but it is already loaded.
The only line that I see you use .add function is at the end where you run:
connection.connect.add(article)
So my guess is that this Model is already loaded in the session and you don't need to add it again. You can add a try, except and rollback the operation if it throws an exception.
Had the same issue, not sure you implemented the models as same as I did, but in my case at least, I had in the table's model - i.e:
product_items = relationship(...)
So later when I tried to do
products = session.query(Products).all()
one_of_the_products = products[0]
new_product = ProductItem(product_id=one_of_the_products.id, name='foo', category='bla')
session.add(new_product)
It raises the same exception as you:
sqlalchemy.exc.InvalidRequestError: Can't attach instance <ProductItem at 0x7fe8d8000e48>; another instance with key (<class 'packages.databases.models.ProductItem'>, (26,), None) is already present in this session.
The reason for the exception, is that when I queried for products - the relationship created it's own sub-query and attached the product_item's objects, it placed them in the variable name I defined in the relationship() -> product_items.
So instead of doing:
session_add(new_product)
I just had to use the relationship:
one_of_the_products.product_items.append(new_product)
session.commit()
hope it helps others.
unloading all objects from session and then adding it again in session might help.
db.session.expunge_all()
db.session.add()

'NoneType' object has no attribute 'group'

Can somebody help me with this code? I'm trying to make a python script that will play videos and I found this file that download's Youtube videos. I am not entirely sure what is going on and I can't figure out this error.
Error:
AttributeError: 'NoneType' object has no attribute 'group'
Traceback:
Traceback (most recent call last):
File "youtube.py", line 67, in <module>
videoUrl = getVideoUrl(content)
File "youtube.py", line 11, in getVideoUrl
grps = fmtre.group(0).split('&')
Code snippet:
(lines 66-71)
content = resp.read()
videoUrl = getVideoUrl(content)
if videoUrl is not None:
print('Video URL cannot be found')
exit(1)
(lines 9-17)
def getVideoUrl(content):
fmtre = re.search('(?<=fmt_url_map=).*', content)
grps = fmtre.group(0).split('&')
vurls = urllib2.unquote(grps[0])
videoUrl = None
for vurl in vurls.split('|'):
if vurl.find('itag=5') > 0:
return vurl
return None
The error is in your line 11, your re.search is returning no results, ie None, and then you're trying to call fmtre.group but fmtre is None, hence the AttributeError.
You could try:
def getVideoUrl(content):
fmtre = re.search('(?<=fmt_url_map=).*', content)
if fmtre is None:
return None
grps = fmtre.group(0).split('&')
vurls = urllib2.unquote(grps[0])
videoUrl = None
for vurl in vurls.split('|'):
if vurl.find('itag=5') > 0:
return vurl
return None
You use regex to match the url, but it can't match, so the result is None
and None type doesn't have the group attribute
You should add some code to detect the result
If it can't match the rule, it should not go on under code
def getVideoUrl(content):
fmtre = re.search('(?<=fmt_url_map=).*', content)
if fmtre is None:
return None # if fmtre is None, it prove there is no match url, and return None to tell the calling function
grps = fmtre.group(0).split('&')
vurls = urllib2.unquote(grps[0])
videoUrl = None
for vurl in vurls.split('|'):
if vurl.find('itag=5') > 0:
return vurl
return None
Just wanted to mention the newly walrus operator in this context because this question is marked as a duplicate quite often and the operator may solve this very easily.
Before Python 3.8 we needed:
match = re.search(pattern, string, flags)
if match:
# do sth. useful here
As of Python 3.8 we can write the same as:
if (match := re.search(pattern, string, flags)) is not None:
# do sth. with match
Other languages had this before (think of C or PHP) but imo it makes for a cleaner code.
For the above code this could be
def getVideoUrl(content):
if (fmtre := re.search('(?<=fmt_url_map=).*', content)) is None:
return None
...
just wanted to add to the answers, a group
of data is expected to be in a sequence, so you can
match each section of the grouped data without
skipping over a data because if a word is skipped from a
sentence, we may not refer to the sentence as one group anymore, see the below example for more clarification, however, the compile method is deprecated.
msg = "Malcolm reads lots of books"
#The below code will return an error.
book = re.compile('lots books')
book = re.search(book, msg)
print (book.group(0))
#The below codes works as expected
book = re.compile ('of books')
book = re.search(book, msg)
print (book.group(0))
#Understanding this concept will help in your further
#researchers. Cheers.

Categories