quota.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. from __future__ import absolute_import
  2. class Quota(object):
  3. """An upper or lower bound for metrics"""
  4. def __init__(self, bound, is_upper):
  5. self._bound = bound
  6. self._upper = is_upper
  7. @staticmethod
  8. def upper_bound(upper_bound):
  9. return Quota(upper_bound, True)
  10. @staticmethod
  11. def lower_bound(lower_bound):
  12. return Quota(lower_bound, False)
  13. def is_upper_bound(self):
  14. return self._upper
  15. @property
  16. def bound(self):
  17. return self._bound
  18. def is_acceptable(self, value):
  19. return ((self.is_upper_bound() and value <= self.bound) or
  20. (not self.is_upper_bound() and value >= self.bound))
  21. def __hash__(self):
  22. prime = 31
  23. result = prime + self.bound
  24. return prime * result + self.is_upper_bound()
  25. def __eq__(self, other):
  26. if self is other:
  27. return True
  28. return (type(self) == type(other) and
  29. self.bound == other.bound and
  30. self.is_upper_bound() == other.is_upper_bound())
  31. def __ne__(self, other):
  32. return not self.__eq__(other)