AdjectiveService.py 793 B

12345678910111213141516171819202122232425262728293031
  1. import random
  2. import falcon
  3. class Adjectives:
  4. def __init__(self):
  5. f = open('adjectives1.txt')
  6. self.wordlist = f.readlines()
  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. app = api = falcon.API()
  24. basicResponder = AdjectiveGetter()
  25. api.add_route('/basic', basicResponder)