answerer.py 1.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. from functools import reduce
  2. from operator import mul
  3. from flask_babel import gettext
  4. keywords = ('min',
  5. 'max',
  6. 'avg',
  7. 'sum',
  8. 'prod')
  9. # required answerer function
  10. # can return a list of results (any result type) for a given query
  11. def answer(query):
  12. parts = query.query.split()
  13. if len(parts) < 2:
  14. return []
  15. try:
  16. args = list(map(float, parts[1:]))
  17. except:
  18. return []
  19. func = parts[0]
  20. answer = None
  21. if func == 'min':
  22. answer = min(args)
  23. elif func == 'max':
  24. answer = max(args)
  25. elif func == 'avg':
  26. answer = sum(args) / len(args)
  27. elif func == 'sum':
  28. answer = sum(args)
  29. elif func == 'prod':
  30. answer = reduce(mul, args, 1)
  31. if answer is None:
  32. return []
  33. return [{'answer': str(answer)}]
  34. # required answerer function
  35. # returns information about the answerer
  36. def self_info():
  37. return {'name': gettext('Statistics functions'),
  38. 'description': gettext('Compute {functions} of the arguments').format(functions='/'.join(keywords)),
  39. 'examples': ['avg 123 548 2.04 24.2']}