AttributeError: 'dict' object has no attribute 'decode' - python

I am trying to run a service even when API link is down but I keep getting the following error:
AttributeError: 'dict' object has no attribute 'decode'.
How can I solve this problem? Thank you
*#app.route('/test_api')
def test_api():
ipno = "192.168.0.120"
port = "8060"
url_time = 'https://{}:{}/time/'.format(ipno, port)
url_member = 'https://{}:{}/member/'.format(ipno, port)
url_state = 'https://{}:{}/state/'.format(ipno, port)
try:
r_time = _session.get(url_time).content
r_member = _session.get(url_member).content
r_state = _session.get(url_state).content
except requests.exceptions.ConnectionError:
r_uptime = {
"uptime": "OFF"
}.decode(utf-8)
r_member ={
"groupCount": "OFF",
}.decode(utf-8)
r_state={ "state": "OFF"
}.decode(utf-8)
return render_template('test_api.html', time = json.loads(r_time), member=json.loads(r_member), state=json.loads(r_state))*

Your code seems to have lots of errors.
First, you are using different names for same variable: r_time and r_uptime
Second, you are using different types for r_xxx values: strings in try and dict in except clause. I think, following code will better suite your needs:
import json.decoder
...
try:
r_uptime = json.loads(_session.get(url_time).content)
r_member = json.loads(_session.get(url_member).content)
r_state = json.loads(_session.get(url_state).content)
except (json.decoder.JSONDecodeError, requests.exceptions.ConnectionError) as ex:
r_uptime = {"uptime": "OFF"}
r_member = {"groupCount": "OFF"}
r_state = {"state": "OFF"}
return render_template('test_api.html', time = r_uptime, member=r_member, state=r_state)

Related

(Pymongo) AttributeError: 'InsertManyResult' object has no attribute 'inserted_count'

I follow the manual https://api.mongodb.com/python/3.4.0/api/pymongo/results.html
and similar problem AttributeError: 'dict' object has no attribute 'is_active' (PyMongo And Flask) (not fit mine issue)
after successfully .insert_many or .insert_one,
the .inserted_count not working
function main part I stock
if one_or_many_bool == True:
x = mycol.insert_many(insert_values_json)
else:
x = mycol.insert_one(insert_values_json)
return x
print(x)
print(x.inserted_count, "documents insert.")
output:
<pymongo.results.InsertManyResult object at 0x0000017D1D256950>
Traceback (most recent call last):
File "C:\Users\chuan\OneDrive\Desktop\10.17_connect_mongoD_練習\fake02.py", line 54, in <module>
print(x.inserted_count, "documents inserted.")
AttributeError: 'InsertManyResult' object has no attribute 'inserted_count'
the whole code(incase someone want to take a look):
import pymongo
import datetime
import json
from bson.objectid import ObjectId
from bson import json_util
def init_db(ip, db, coll):
try:
myclient = pymongo.MongoClient('mongodb://' + ip + '/')
mydb = myclient[db]
mycol = mydb[coll]
except Exception as e:
msg_fail_reason = "error in init_db function"
return msg_fail_reason
return mydb, mycol
# ins_data = insert_db_data
# one_or_many_bool: input 1 means True; input 0 is False
def ins_data(one_or_many_bool, insert_values_json ):
try:
if one_or_many_bool:
x = mycol.insert_many(insert_values_json)
else:
x = mycol.insert_one(insert_values_json)
return x
except Exception as e:
msg_fail_reason = "error in ins_data function"
return msg_fail_reason
msg_fail_reason = "no error occur"
ip_input = input("Enter the ip: ")
exist_DB_name = input("Enter exist DB name: ")
exist_coll_name = input("Enter exist collection name: ")
mydb, mycol = init_db(ip_input, exist_DB_name, exist_coll_name)
update_one_or_many = input("U are update one or many values? (ex:1 for many , 0 for one): ")
newvalues_str = input("Enter new values: ")
one_or_many_bool = bool(int(update_one_or_many))
insert_values_json =json.loads(newvalues_str)
x = ins_data(one_or_many_bool, insert_values_json )
print(x)
print(x.inserted_count, "documents insert.")
number_of_insert_data = int(x.inserted_count)
modified_data_list = []
for modified_data in mycol.find().sort("_id", -1).limit(number_of_insert_data):
# print(modified_data)
modified_data_list.append(modified_data)
def parse_json(data):
return json.loads(json_util.dumps(data))
# if someone want data in json
modified_data_json = parse_json(modified_data_list)
# 1 means success
return_status_str = { "ok" : 1 , "msg" : msg_fail_reason , "count" : number_of_insert_data}
print(return_status_str)
print(type(return_status_str))
As the PyMongo doc described, InsertManyResult doesn't has the inserted_count attrbute. You can use len(result.inserted_ids) instead.

TypeError: Object of type TypeError is not JSON serializable Python

So I have JSON request with format like this:
{
"item_ids": [635,10,692,194,9412],
"gender": "male",
"number_results": 5
}
I'm trying to parse array in "item_ids". But I got error message like in the title. This is my code:
resto_id = json.loads['item_ids']
data = json.dumps(resto_id)
I also tried this:
response = requests.get("http://127.0.0.1:8520/recommend_multi")
users = json.loads(response.text)
data = users['item_ids']
But gave me an error:
TypeError: Object of type JSONDecodeError is not JSON serializable
Edit: Maybe this will help:
#app.route('/recommend_multi', methods=['POST'])
def recommend_multi():
dct={}
new_user = 'newusername'
try:
e=""
resto_id = json.loads['item_ids']
data = json.dumps(resto_id)
# response = requests.get("http://127.0.0.1:8520/recommend_multi")
# users = json.loads(response.text)
# data = users['item_ids']
gender = request.json['gender']
resto_rec = float(request.json['number_results'])
input_dict = {'id_resto': data,
'gender': [gender, gender, gender, gender, gender], 'username': [new_user, new_user, new_user, new_user, new_user]}
dct = {"items": input_dict}
dct2 = {"data": dct, "message":"sukses", "success":True}
except Exception as e:
dct2 = {"data": dct, "message":e, "success":False}
return jsonify(dct2)
And this is the traceback:
I run it with docker. And for request I'm using Insomnia
The problem is in this snippet:
except Exception as e:
dct2 = {"data": dct, "message":e, "success":False}
You are basically trying to JSON serialize the exception e which is not possible. You need to use something that is JSON serializable, like the string representation of the exception, for example by using str(e):
except Exception as e:
dct2 = {"data": dct, "message":str(e), "success":False}
First, thanks to #bdbd to keep responding to me. The exception solution helped me to fix my code, but in the end I managed to debug my code then found the solution which is resolve my problems about retrieving array in JSON objects. So instead of this:
resto_id = json.loads['item_ids']
data = json.dumps(resto_id)
I need to request first.
resto_id = request.json
data = json.dumps(resto_id)
data2 = json.loads(data)
#Then retrieve the array
data2["item_ids"]

Object is not subscriptable in django

I have this function in django:
def get_customer(request):
resp = dict(succees=False, message='no se encontro clientes')
database = request.user.company_select.company_db
try:
customers = Customer.objects.using(database).get(id_customers=request.data['id_customer'])
if customers:
list_customers = list()
customers['subgroup_p'] = Customer_Subgroup.objects.using(database).values().filter(id_subgroup=customers['customers_subgroup'])
customers['group_p'] = Customer_Group.objects.using(database).values().filter(id_group=customers['subgroup_p']['group_id'])
customers['customers_subgroup_p'] = Catalog_Type_Customer.objects.using(database).values().filter(id_type_customer=customers['customers_type'])
customers['city_p'] = City.objects.values().filter(city_id=customers['city'])
customers['state_p'] = State.objects.values().filter(state_id=customers['city_p']['state_id'])
customers['country_p'] = Country.objects.values().filter(country_id=customers['state_p']['country_id'])
list_customers.append(customers)
resp.update(dict(success=True, message='', customers=list_customers))
except Exception as e:
print(e)
resp.update(dict(message='Error'))
return Response(resp)
But i get the error ´Customer´ is not subscriptable
What can i do to solve this?
Thanks!
You're trying to access a django model object property as dict but you need to access it as a property using .property_name like customers.subgroup_p.
try this:
def get_customer(request):
resp = dict(succees=False, message='no se encontro clientes')
database = request.user.company_select.company_db
try:
customers = Customer.objects.using(database).get(id_customers=request.data['id_customer'])
if customers:
list_customers = list()
customers.subgroup_p = Customer_Subgroup.objects.using(database).values().filter(id_subgroup=customers.customers_subgroup)
customers.group_p = Customer_Group.objects.using(database).values().filter(id_group=customers.subgroup_p.group_id)
customers.customers_subgroup_p = Catalog_Type_Customer.objects.using(database).values().filter(id_type_customer=customers.customers_type)
customers.city_p = City.objects.values().filter(city_id=customers.city)
customers.state_p = State.objects.values().filter(state_id=customers.city_p.state_id)
customers.country_p = Country.objects.values().filter(country_id=customers.state_p.country_id)
list_customers.append(customers)
resp.update(dict(success=True, message='', customers=list_customers))
except Exception as e:
print(e)
resp.update(dict(message='Error'))
return Response(resp)

Exception not raised assertRaises()?

I am giving a wrong input and I want an exception to be raised. Somehow this is not happening. This is my unit test code:
def test_invalid_bag_json_conversion_1(self):
file_name = "../test/test_files/wrong_bag.bag"
ru = RosbagUploader(file_name, self.json_output, "", "", "", "")
status = ru.start()
self.assertRaises(Exception, RosbagUploader, file_name, self.json_output, "", "", "", "")
self.assertEquals(ReturnCodes.FAIL, status)
and my code that I am testing:
class RosbagUploader(object):
"""
#brief Uploads deserialized input Rosbag file to ElasticSearch or
stores it locally
"""
def __init__(self, _rosbag_filepath, _json_filename, _es_addr, _es_index,
_es_type, _buffer_size):
self.overall_status = ReturnCodes.SUCCESS
self._rosbag_filepath = _rosbag_filepath
self._json_filename = _json_filename
self._buffer_size = _buffer_size if _buffer_size > 0 else 5000
self._es_type = _es_type
self._es_addr = _es_addr
self._es_index = _es_index
self._es_buff = []
try:
self._rosbag = rosbag.Bag(_rosbag_filepath, "r")
if self._es_addr:
self._es = Elasticsearch() if _es_addr == "" else \
Elasticsearch([_es_addr])
self._total_num_record = self._rosbag.get_message_count()
except:
print("[ERROR] {}".format(sys.exc_info()))
self.overall_status = ReturnCodes.FAIL
It shows the output that the exception is raised as below:
[ERROR] (<class 'rosbag.bag.ROSBagException'>, ROSBagException(), <traceback object at 0x7fdcb463e8c0>)
EException AttributeError: "'RosbagUploader' object has no attribute '_rosbag'" in <bound method RosbagUploader.__del__ of <rosbag_deserializer_core.RosbagUploader object at 0x7fdcb4899ad0>> ignored
Which is what it should do. But why doesnt it raise the exception?
You are essentially ignoring the exception since you are not re-raising after the print statement:
try:
[...]
except:
print("[ERROR] {}".format(sys.exc_info()))
self.overall_status = ReturnCodes.FAIL
You need to re-raise for callers to receive the exception:
try:
[...]
except:
print("[ERROR] {}".format(sys.exc_info()))
self.overall_status = ReturnCodes.FAIL
raise
Do you have a __del__ destruction method? Seems to be failing there.

Pickleing error: connot pickle Request object

I know That it is not possible to pickle a pyramid request object, but I cant seem to find where I am sending the Request object.
Consider the following:
#task
def do_consignment_task(store, agent):
print "GOTHERE IN TASK"
s = sqlahelper.get_session()
consign = store.gen_consignment()
ca = Agents.by_id(store.consignment_agents_id)
consign.consignment_agents_id = ca.id
consign.consignment_teamleader_id = ca.ou[0].lead_agents_id
consign.consignment_timestamp = func.now()
consign.created_by_agent_id = agent.id
consign.complete_stamp = func.now()
consign.sims = store.sims
consign.status = "SUCCESS"
print "GOT BEFORE LOOP "
for sim in store.sims:
if sim in consign.sims:
continue
else:
consign.sims.append(sim)
s.add(consign)
transaction.savepoint()
print "GOT AFTER SAVEPOINT"
for sim in consign.sims:
is_reconsign = sim.consignment_agent or sim.consignment_teamlead
if is_reconsign:
if not sim.consignment_history:
sim.consignment_history = []
sim.consignment_history.append(dict(
stamp=sim.consignment_timestamp,
consignment_agent_id=sim.consignment_agents_id,
consignment_teamleader_id=sim.consignment_teamleader_id,
by_agent_id=agent.id
))
s.query(
Sims
).filter(
Sims.iccid == sim.iccid
).update(
{
"consignment_agents_id": consign.consignment_agents_id,
"consignment_history": sim.consignment_history,
"consignment_teamleader_id": ca.ou[0].lead_agents_id,
"consignment_timestamp": func.now(),
"modify_stamp": func.now(),
"consignments_id": consign.id
},
synchronize_session=False
)
print "GOT BEFORE COMMIT"
transaction.savepoint()
print "THIS IS THE ID ID ID ID ID ID : ", consign.id
I call this function like:
if self.store.finalise:
try:
store = self.store
agent = self.agent
do_consignment_task.delay(store, agent)
transaction.commit()
self.check_and_purge()
return "Consignmnet is being processed"
except Exception, exc:
self.check_and_purge()
self.log.exception(exc)
exc_error = "CONSIGNERR:", exc.message
raise USSDFailure(exc_error)
else:
self.store.status = "CANCELLED"
if "fullconfirm" in self.session:
del self.session["fullconfirm"]
self.check_and_purge()
return "CONSIGNMENT Cancelled"
When I run this code I get the following error:
EncodeError: Can't pickle <class 'pyramid.util.Request'>: attribute lookup pyramid.util.Request failed
I am not sending self or request objects - at least not that I can see.
How can solve this problem? Am I sending a request object, because I can not see one?
The traceback can be seen here
EDIT:
okay So I have tried to change the data I send to the function - I am not passing a sqlalchemy object and I am making a copy of the store object, that changes my code to:
#task
def do_consignment_task(agent_id, **store):
print "GOTHERE IN TASK"
s = sqlahelper.get_session()
cObj = USSDConsignmentsObject()
consign = cObj.gen_consignment()
ca = Agents.by_id(store.consignment_agents_id)
consign.consignment_agents_id = ca.id
consign.consignment_teamleader_id = ca.ou[0].lead_agents_id
consign.consignment_timestamp = func.now()
consign.created_by_agent_id = agent_id
# etc
and:
if self.store.finalise:
try:
# del self.service
store = self.store.__dict__.copy()
agent_id = self.agent.id
print store
print agent_id
# print help(store)
do_consignment_task.delay(agent_id, **store)
transaction.commit()
#etc
This however still gives me the same error :|
Try not to serialise a Pyramid request object. When you interact with a celery task you should think of it as an independent process.
Provide it all the information it needs to do it's work. Be aware that you need to serialise that information.
So self.store possibly contains attribute references that may be unrealistic to serialise.
Perhaps create a method on the store object that returns a clean dictionary object.
def serialize(self):
data = {}
data["element1"] = self.element1
data["element2"] = self.element2
data["element3"] = self.element3
return data
Then when you want to call the delay method make sure to use store.serialize() instead of store or the dict.

Categories