possibilities.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  1. # Each rule will append its hint message to the following dictionary. The
  2. # function pointer to the apply function of the rule is used as key. The
  3. # corresponding value is a string, which will be used to produce the hint
  4. # message. The string will be processed using string.format().
  5. MESSAGES = {}
  6. class Possibility(object):
  7. def __init__(self, root, handler, args=()):
  8. self.root = root
  9. self.handler = handler
  10. self.args = args
  11. def __str__(self):
  12. if self.handler in MESSAGES:
  13. return MESSAGES[self.handler].format(self.root, *self.args)
  14. return '<Possibility root="%s" handler=%s args=%s>' \
  15. % (self.root, self.handler.func_name, self.args)
  16. def __repr__(self):
  17. return '<Possibility root="%s" handler=%s args=%s>' \
  18. % (self.root, self.handler.func_name, self.args)
  19. # TODO: Add unit tests
  20. def __eq__(self, other):
  21. return self.handler == other.handler \
  22. and hash(self.root) == hash(other.root) \
  23. and self.args == other.args
  24. def filter_duplicates(items):
  25. unique = []
  26. for item in items:
  27. found = False
  28. for compare in unique:
  29. if item == compare:
  30. found = True
  31. break
  32. if not found:
  33. unique.append(item)
  34. return unique
  35. def pick_suggestion(possibilities):
  36. # TODO: pick the best suggestion.
  37. suggestion = 0
  38. return possibilities[suggestion]
  39. def apply_suggestion(suggestion):
  40. return suggestion.handler(suggestion.root, suggestion.args)