Advertisement
tyler569

fib as a service

Apr 2nd, 2019
417
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.54 KB | None | 0 0
  1. import functools
  2.  
  3. from flask import Flask, Response
  4. app = Flask(__name__)
  5.  
  6. @functools.lru_cache(maxsize=None)
  7. def fibonacci(i):
  8.     a, b = 0, 1
  9.     for j in range(i):
  10.         a, b = b, b+a
  11.  
  12.     return b
  13.  
  14. @app.route('/fib/<number>')
  15. def render_fib(number):
  16.     try:
  17.         num = int(number)
  18.     except Exception:
  19.         return Response("400 Bad Request", status=400)
  20.  
  21.     if num > 500000:
  22.         return Response("403 Forbidden", status=403)
  23.  
  24.     resp = str(fibonacci(num)) + "\n"
  25.     return Response(resp, mimetype='text/plain')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement