NounService.py 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. import falcon
  2. import random
  3. class Nouns:
  4. def __init__(self):
  5. f = open('nouns.txt')
  6. self.wordlist = f.read().splitlines()
  7. random.shuffle(self.wordlist)
  8. self.index = 0
  9. self.shuffletime = (3 * len(self.wordlist)) / 5
  10. def getWord(self):
  11. self.index += 1
  12. if self.index > self.shuffletime:
  13. random.shuffle(self.wordlist)
  14. self.index = 0
  15. return self.wordlist[self.index]
  16. class NounGetter:
  17. def __init__(self):
  18. self.words = Nouns()
  19. def on_get(self, req, resp):
  20. """Handle GET requests"""
  21. resp.status = falcon.HTTP_200
  22. resp.body = self.words.getWord()
  23. class NounGetterJSON:
  24. def __init__(self):
  25. self.words = Nouns()
  26. def on_get(self, req, resp):
  27. """Handle GET requests"""
  28. resp.status = falcon.HTTP_200
  29. chosenWord = self.words.getWord()
  30. resp.body = '{{"noun":"{0}"}}'.format(chosenWord)
  31. class NounGetterXML:
  32. def __init__(self):
  33. self.words = Nouns()
  34. def on_get(self, req, resp):
  35. """Handle GET requests"""
  36. resp.status = falcon.HTTP_200
  37. chosenWord = self.words.getWord()
  38. resp.body = '<noun>{0}</noun>'.format(chosenWord)