Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Flask: TypeError: 'int' object is not callable [duplicate]

I'm new to Python and just learning it as I work on this project, and this issue is really confusing me. Here's my code:

from flask import Flask
from datetime import datetime

# Setup app
app = Flask(__name__)

# Initialize data dict
data = {}

# Pretty-formats a time difference
def formatdifference(delta):
    seconds = delta.total_seconds()
    if (seconds < 60):
        return "{seconds} seconds ago" % {"seconds": seconds}

    return "{minutes} minutes ago" % {"minutes": seconds/60}

# Sets a device battery level
@app.route("/set/<device>/<int:battery>")
def set(device, battery):
    data[device] = (battery, datetime.now())
    return "done"

# Get's a device battery level
@app.route("/get/<device>")
def get(device):
    if not device in data:
        return "No heartbeats"

    devicedata = data[device]
    delta = datetime.now() - devicedata[1]

    if (delta.total_seconds() > 10):
        return "Last heartbeat {diff}" % {"diff": formatdifference(delta)}

    return devicedata[0]

if __name__ == "__main__":
    app.run(debug=True, host='0.0.0.0')

I call /set/phone/40. All good so far. Then I call /get/phone. Not so good. Here's the traceback:

TypeError: 'int' object is not callable
Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1836, in __call__
    return self.wsgi_app(environ, start_response)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1820, in wsgi_app
    response = self.make_response(self.handle_exception(e))
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1403, in handle_exception
    reraise(exc_type, exc_value, tb)
  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 1478, in full_dispatch_request
    response = self.make_response(rv)
  File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1577, in make_response
    rv = self.response_class.force_type(rv, request.environ)
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/wrappers.py", line 825, in force_type
    response = BaseResponse(*_run_wsgi_app(response, environ))
  File "/usr/local/lib/python2.7/dist-packages/werkzeug/test.py", line 855, in run_wsgi_app
    app_iter = app(environ, start_response)
TypeError: 'int' object is not callable

Using flask's debugger, I can see that in the last frame, app appears to be 40!

How on earth did this happen, and how can I solve it?

like image 455
JJJollyjim Avatar asked Jun 07 '14 09:06

JJJollyjim


1 Answers

You should return a string object, not an int object:

Replace the following line:

return devicedata[0]

with:

return str(devicedata[0])

Or, replace the following line:

data[device] = (battery, datetime.now())

with:

data[device] = (str(battery), datetime.now())
like image 187
falsetru Avatar answered Oct 25 '22 01:10

falsetru