Talk given at Datapalooza Denver 2016 titled "(Your) Data as a Service: The Easy Way to Build an API for Your Data".
Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

api_server.py 2.7 KiB

il y a 8 ans
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. # -*- coding: utf-8 -*-
  2. from datetime import datetime
  3. import sys
  4. import connexion
  5. def root():
  6. cur_time = datetime.now().isoformat()
  7. return {'name': 'demo-data-one', 'time': cur_time}
  8. def get_summary_uris():
  9. years = xrange(2007,2015)
  10. return [{'year': y, 'link': "/summary/costs/{}".format(y)} for y in years]
  11. def get_summary(year):
  12. from xlrd import open_workbook, XL_CELL_TEXT
  13. try:
  14. if year in xrange(2007, 2011):
  15. book = open_workbook('data/ic2010_ay.xls')
  16. sheet = book.sheet_by_name('Statistics')
  17. in_state_tuition = {
  18. # the tuple is : in-state tuition + fees, books/supplies
  19. 2007: (sheet.cell(74, 4).value, sheet.cell(100, 4).value),
  20. 2008: (sheet.cell(77, 4).value, sheet.cell(101, 4).value),
  21. 2009: (sheet.cell(80, 4).value, sheet.cell(102, 4).value),
  22. 2010: (sheet.cell(83, 4).value, sheet.cell(100, 4).value)
  23. }
  24. #
  25. # for demo : uncomment and finish
  26. #
  27. # out_of_state_tuition = {
  28. # '2007': sheet.cell(88, 4).value,
  29. # '2008': sheet.cell(91, 4).value,
  30. # '2009': sheet.cell(94, 4).value,
  31. # '2010': sheet.cell(97, 4).value
  32. # }
  33. return dict(zip(['tuition_and_fees', 'books_and_supplies'], in_state_tuition[year]))
  34. elif year in xrange(2011, 2015):
  35. book = open_workbook('data/ic2014_ay.xlsx')
  36. sheet = book.sheet_by_name('Statistics')
  37. in_state_tuition = {
  38. 2011: (sheet.cell(71, 4).value, sheet.cell(97, 4).value),
  39. 2012: (sheet.cell(74, 4).value, sheet.cell(98, 4).value),
  40. 2013: (sheet.cell(77, 4).value, sheet.cell(99, 4).value),
  41. 2014: (sheet.cell(80, 4).value, sheet.cell(100, 4).value)
  42. }
  43. #
  44. # for demo : uncomment and finish
  45. #
  46. # out_of_state_tuition = {
  47. # '2011': sheet.cell(85, 4).value,
  48. # '2012': sheet.cell(88, 4).value,
  49. # '2013': sheet.cell(91, 4).value,
  50. # '2014': sheet.cell(94, 4).value
  51. # }
  52. return dict(zip(['tuition_and_fees', 'books_and_supplies'], in_state_tuition[year]))
  53. else:
  54. return {"error": "Data for year {} not found.".format(year)}
  55. except IOError:
  56. # Internal Server Error - NOT GOOD, but do something about it!
  57. return {"error": "An internal server error occured.", "reason": sys.exc_info(), "input": "\"{}\"".format(year)}
  58. if __name__ == '__main__':
  59. app = connexion.App(__name__, specification_dir='apispec/')
  60. app.add_api('data_api.yaml')
  61. # start the server
  62. app.run(port=1234)