answerer.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import random
  2. import string
  3. from flask_babel import gettext
  4. # required answerer attribute
  5. # specifies which search query keywords triggers this answerer
  6. keywords = ('random',)
  7. random_int_max = 2**31
  8. random_string_letters = string.lowercase + string.digits + string.uppercase
  9. def random_string():
  10. return u''.join(random.choice(random_string_letters)
  11. for _ in range(random.randint(8, 32)))
  12. def random_float():
  13. return unicode(random.random())
  14. def random_int():
  15. return unicode(random.randint(-random_int_max, random_int_max))
  16. random_types = {u'string': random_string,
  17. u'int': random_int,
  18. u'float': random_float}
  19. # required answerer function
  20. # can return a list of results (any result type) for a given query
  21. def answer(query):
  22. parts = query.query.split()
  23. if len(parts) != 2:
  24. return []
  25. if parts[1] not in random_types:
  26. return []
  27. return [{'answer': random_types[parts[1]]()}]
  28. # required answerer function
  29. # returns information about the answerer
  30. def self_info():
  31. return {'name': gettext('Random value generator'),
  32. 'description': gettext('Generate different random values'),
  33. 'examples': [u'random {}'.format(x) for x in random_types]}