0

I'm deploying simple API written in python (Flask), but I get weird error "TypeError: 'float' object is not callable". However, object is int type, what I confirmed. I tried even enforcing int type by "for counter in range(int(additional_params_count)):" but it didn't help.

Below is the error message and source

 1
 <type 'int'>
 [20130826-16:14PM] [flats_api] [ERROR] Exception on /v1/closest_point [GET]
 Traceback (most recent call last):
   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1817, in wsgi_app
     response = self.full_dispatch_request()
   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1477, in full_dispatch_request
     rv = self.handle_user_exception(e)
   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1381, in handle_user_exception
     reraise(exc_type, exc_value, tb)
   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1475, in full_dispatch_request
     rv = self.dispatch_request()
   File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1461, in dispatch_request
     return self.view_functions[rule.endpoint](**req.view_args)
   File "/var/www/flats_api/flats_api.py", line 104, in closest_point
     for counter in range(0,additional_params_count):
 TypeError: 'float' object is not callable

source:

def closest_point():
         lng = request.args.get('lng', default=None, type=float)
         lat = request.args.get('lat', default=None, type=float)

         price_min = request.args.get('price_min',default=None, type=float)
         price_max = request.args.get('price_max',default=None, type=float)

         rooms_min = request.args.get('rooms_min',default=None, type=float)
         rooms_max = request.args.get('rooms_max',default=None, type=float)

         if (lng != None) and (lat != None):
                 range = 0.1
                 lng_min = lng - range
                 lng_max = lng + range
                 lat_min = lat - range
                 lat_max = lat + range

                 cur = g.db.cursor()
                 query = "SELECT id, link, price, longitude, latitude FROM " + DATABASE_TABLE_NAME + " WHERE latitude >= " + str(lat_min) + " AND latitude <= " + str(lat_max) + " AND longitude >= " + str(lng_min) + " AND longitude <= " + str(lng_max)

                 # I always have 5 basic parameters to group by. I count number of additional parameters to know how many to add
                 additional_params_count = 0
                 if price_min:
                         query = query + " AND price >= " + str(price_min)
                         additional_params_count = additional_params_count + 1
                 query = query + "GROUP BY 1,2,3,4,5"
                 print additional_params_count
                 print type(additional_params_count)
                 for counter in range(0,additional_params_count):
                         print counter
                         query = query + "," + str(counter + 6)
                 query = query + ";"
                 print query
                 cur.execute(query)
                 columns = [desc[0] for desc in cur.description]
                 rows = cur.fetchall()
                 flats_json = []
                 for row in rows:
                         flats_json.append(dict((k,str(v)) for k,v in zip(columns,row)))
                 response_body = str(rows)
                 return json.dumps(flats_json)
         else:
                 return "One of lng or lat parameters missing"

1 Answer 1

3

You assigned range to a float in your code, don't do that otherwise you can't access the range() built-in then:

if (lng != None) and (lat != None):
     range = 0.1       ###########

Demo:

>>> range = 0.1
>>> range()
Traceback (most recent call last):
  File "<ipython-input-39-7b0c968826c1>", line 1, in <module>
    range()
TypeError: 'float' object is not callable
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.