-2

I have this code:

qcmz = session1.query(func.sum(KCom.c1)).filter(KCom.users_id != current_user.id and KCom.ofv_id == cmz_id).all()
    qcmz1 = np.array(qcmz) + 0. 
    qcmz2 = float(qcmz1[0])
    qcmz3 = float("{0:.2g}".format(qcmz2))

And is returning this error:

Traceback (most recent call last):
  File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/flask/app.py", line 1982, in wsgi_app
    response = self.full_dispatch_request()
  File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/flask/app.py", line 1614, in full_dispatch_request
    rv = self.handle_user_exception(e)
  File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/flask/app.py", line 1517, in handle_user_exception
    reraise(exc_type, exc_value, tb)
  File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/flask/_compat.py", line 33, in reraise
    raise value
  File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/flask/app.py", line 1612, in full_dispatch_request
    rv = self.dispatch_request()
  File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/flask/app.py", line 1598, in dispatch_request
    return self.view_functions[rule.endpoint](**req.view_args)
  File "/home/ubuntu/workspace/avb/venv/lib/python3.6/site-packages/flask_login/utils.py", line 228, in decorated_view
    return func(*args, **kwargs)
  File "/home/ubuntu/workspace/avb/app.py", line 822, in fcmz
    qcmz1 = np.array(qcmz) + 0. 
TypeError: unsupported operand type(s) for +: 'NoneType' and 'float'

The ideea is when the db table is populated is not returning any errors only when is empty, I mean when the KCom.c1 asociated to another by ForeignKey and is empty is returning this issue.

Alex
  • 15
  • 5
  • `KCom.users_id != current_user.id and KCom.ofv_id == cmz_id` is not doing what you think: https://stackoverflow.com/questions/42681231/sqlalchemy-boolean-value-of-this-clause-is-not-defined/42695255#42695255, but that's not the source of your error, probably, but another. – Ilja Everilä Aug 15 '18 at 19:44
  • Is qcmz object dtype with one or more None elements? – hpaulj Aug 15 '18 at 19:45
  • yes it is qcmz dtype with None element, but I will explain you qcmz it is a quantity asociated to an order, if the order is not yet the value it is None when the order will be qcmz is getting value, the ideea it is I have to convert the None to 0(zero) to not get this error – Alex Aug 15 '18 at 19:51

2 Answers2

1

The only place you use the + operand in your code is on the following line:

qcmz1 = np.array(qcmz) + 0. 

What does qcmz look like and what are you trying to accomplish by adding + 0. ? Looks like np.array(qcmz)is sometimes a None type and you cannot add a number.

Jen
  • 635
  • 5
  • 9
  • is geting sum of variable c1 from KCOM.c1 asociated to each row by users_id, this is the ideea when is None to be 0 (zero) to not return the error. – Alex Aug 15 '18 at 19:35
  • So you want to replace all the None values in the array with a 0? You would do this: `qcmz = [qcmz == None] = 0.0`. Then you need to convert the type to float like this `qcmz.astype(float)` – Jen Aug 15 '18 at 20:17
  • thank you for your answer but I was finding the solution. Thank you for all for support! – Alex Aug 15 '18 at 20:19
0

This is working now well:

qcmz = session1.query(func.sum(KCom.c1)).filter(KCom.users_id != current_user.id and KCom.ofv_id == cmz_id).all()
    qcmz0 = np.array(qcmz)
    if qcmz0 == None:
        qcmz3 = 0
    else:
        qcmz1 = qcmz0 + 0. # conversia din list in float cu numpy
        qcmz2 = float(qcmz1[0])
        qcmz3 = float("{0:.2g}".format(qcmz2))
Alex
  • 15
  • 5