AdjectiveService.py 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. import falcon
  2. import random
  3. class Adjectives:
  4. def __init__(self):
  5. f = open('adjectives1.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 AdjectiveGetter:
  17. def __init__(self):
  18. self.words = Adjectives()
  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 AdjectiveGetterJSON:
  24. def __init__(self):
  25. self.words = Adjectives()
  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 = '{{"adjective":"{0}"}}'.format(chosenWord)
  31. class AdjectiveGetterXML:
  32. def __init__(self):
  33. self.words = Adjectives()
  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 = '<adjective>{0}</adjective>'.format(chosenWord)