backend.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. import web
  2. import json
  3. from src.parser import Parser
  4. from tests.parser import ParserWrapper
  5. from src.validation import validate as validate_expression
  6. urls = (
  7. '/math\.py/validate', 'validate',
  8. '/math\.py/hint', 'hint',
  9. '/math\.py/step', 'Step',
  10. '/math\.py/answer', 'Answer',
  11. )
  12. def get_last_line():
  13. data = web.input(data='').data
  14. lines = map(str, data.split('\n'))
  15. # Get the last none empty line.
  16. for i in range(len(lines))[::-1]:
  17. last_line = lines[i].strip()
  18. if last_line:
  19. return last_line
  20. class Step(object):
  21. def POST(self):
  22. web.header('Content-Type', 'application/json')
  23. try:
  24. last_line = get_last_line()
  25. if last_line:
  26. parser = ParserWrapper(Parser)
  27. response = parser.run([last_line])
  28. if response:
  29. response = parser.rewrite(include_step=True,
  30. check_implicit=True)
  31. if response:
  32. hint, step = response
  33. return json.dumps({'step': str(step),
  34. 'hint': str(hint)})
  35. return json.dumps({'hint': 'No further reduction is possible.'})
  36. except Exception as e:
  37. return json.dumps({'error': str(e)})
  38. class Answer(object):
  39. def POST(self):
  40. web.header('Content-Type', 'application/json')
  41. try:
  42. last_line = get_last_line()
  43. if last_line:
  44. parser = ParserWrapper(Parser)
  45. response = parser.run([last_line])
  46. if response:
  47. steps = parser.rewrite_all(include_steps=True)
  48. if steps:
  49. out = []
  50. for h, s in steps:
  51. out.append(dict(hint=str(h), step=str(s)))
  52. return json.dumps({'steps': out})
  53. return json.dumps({'hint': 'No further reduction is possible.'})
  54. except Exception as e:
  55. return json.dumps({'error': str(e)})
  56. class hint(object):
  57. def POST(self):
  58. web.header('Content-Type', 'application/json')
  59. try:
  60. last_line = get_last_line()
  61. if last_line:
  62. parser = ParserWrapper(Parser)
  63. response = parser.run([last_line])
  64. response = parser.parser.give_hint()
  65. if response:
  66. return json.dumps({'hint': str(response)})
  67. return json.dumps({'hint': 'No further reduction is possible.'})
  68. except Exception as e:
  69. return json.dumps({'error': str(e)})
  70. class validate(object):
  71. def POST(self):
  72. web.header('Content-Type', 'application/json')
  73. data = web.input(data='').data
  74. lines = map(str, data.split('\n'))
  75. i = 0
  76. skipped = 0
  77. try:
  78. # Get the first none empty line.
  79. for i in range(0, len(lines)):
  80. last_line = lines[i].strip()
  81. if not last_line: # or last_line in ['?']:
  82. skipped += 1
  83. continue
  84. break
  85. # Validate each none empty line with the following none empty line.
  86. for i in range(i + 1, len(lines)):
  87. line = lines[i].strip()
  88. if not line: # or line in ['?']:
  89. skipped += 1
  90. continue
  91. if not validate_expression(last_line, line):
  92. i -= 1
  93. break
  94. last_line = line
  95. return json.dumps({'validated': i - skipped})
  96. except Exception as e:
  97. i -= 1
  98. return json.dumps({'error': str(e), 'validated': i - skipped})
  99. if __name__ == "__main__":
  100. app = web.application(urls, globals(), autoreload=True)
  101. app.run()