possibilities.py 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. class Possibility(object):
  2. def __init__(self, root, handler, args):
  3. self.root = root
  4. self.handler = handler
  5. self.args = args
  6. def __str__(self):
  7. return '<Possibility root="%s" handler=%s args=%s>' \
  8. % (self.root, self.handler.func_name, self.args)
  9. def __repr__(self):
  10. return str(self)
  11. # TODO: Add unit tests
  12. def __eq__(self, other):
  13. return self.handler == other.handler \
  14. and hash(self.root) == hash(other.root) \
  15. and self.args == other.args
  16. def filter_duplicates(items):
  17. unique = []
  18. for item in items:
  19. found = False
  20. for compare in unique:
  21. if item == compare:
  22. found = True
  23. break
  24. if not found:
  25. unique.append(item)
  26. return unique
  27. def pick_suggestion(possibilities):
  28. # TODO: pick the best suggestion.
  29. suggestion = 0
  30. return possibilities[suggestion]
  31. def apply_suggestion(suggestion):
  32. return suggestion.handler(suggestion.root, suggestion.args)